0

I have an issue with opening a php file in a side folder for a project. I'm using

include('$_SERVER[DOCUMENT_ROOT]/KungFu/php/hours.php')

The error message is:

"Warning: include(1): failed to open stream: No such file or directory in C:\xampp\htdocs\KungFu\index.php on line 48"

also tried every variation there can be - with/out $_SERVER[DOCUMENT_ROOT], it still is not working. The index.php is in the main folder, and in the same one in a subfolder I have the hours.php, but it is not opening it in an include statement. However it is opening it via an a tag <a> with /KungFu/php/hours.php, so I really dont udnerstand why.

3
  • include($_SERVER[DOCUMENT_ROOT].'/KungFu/php/hours.php'); Commented Dec 30, 2019 at 13:42
  • try relative to your current file.. include (__DIR__.'/../KungFu/php/hours.php') (or include (__DIR__.'/KungFu/php/hours.php') if i have mistaken the folder structure) Commented Dec 30, 2019 at 13:53
  • no it still doesnt work, tried all of this Commented Dec 30, 2019 at 15:08

2 Answers 2

1

There are a couple of things wrong with your code.

To start, you cannot use variables inside single quotes. You can use them within double quotes. However I would suggest to use concatenation.

Also you have to put DOCUMENT_ROOT between single quotes because it's a key of an array (string) and not a constant. If you have error reporting enabled you should see a warning like this:

Use of undefined constant DOCUMENT_ROOT - assumed 'DOCUMENT_ROOT' (this will throw an Error in a future version of PHP) ......

Correct example:

include($_SERVER['DOCUMENT_ROOT'] . '/KungFu/php/hours.php');

Knowing the path to the file is C:\xampp\htdocs\KungFu\php\hours.php and the path to the current file is C:\xampp\htdocs\KungFu\index.php you can use dirname() instead of the $_SERVER superglobal:

include(dirname(__FILE__) . '/php/hours.php');
Sign up to request clarification or add additional context in comments.

12 Comments

You talk about an error, but there's no error message in your question. Please add it to your question if there is one!
"Warning: include(1): failed to open stream: No such file or directory in C:\xampp\htdocs\KungFu\index.php on line 48" thats the exact message im getting
All right, that's something we can work with! Please post the results of var_dump($_SERVER['DOCUMENT_ROOT']). With that information we can check if the path is the right one!
string(15) "C:/xampp/htdocs" this is what your code returns !
I added an example to use dirname(), please check!
|
0

If you directory something like this.

  • app

    • index.php

    • kungfu

      • php

        • hours.php

then simple include file like this no need to add global variable

include "kungfu/php/hours.php";

2 Comments

index.php is in KungFu folder
Then do something like this. include "php/hours.php";