Home » Php » php – file_get_contents with relative path

php – file_get_contents with relative path

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have the following directory structure.

/var/www/base/controller/detail.php
/var/www/base/validate/edit.json
/var/www/html

Within /var/www/base/controller/detail.php, how do I use file_get_contents() with a relative path to read /var/www/base/validate/edit.json? I’ve tried the following:

//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('detail.php');

//No error, but I don't want this file and was just testing
$json=file_get_contents('detail.php', FILE_USE_INCLUDE_PATH);

//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('./validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('././validate/edit.json', FILE_USE_INCLUDE_PATH);
//failed to open stream: No such file or directory (error no: 2)
$json=file_get_contents('../../validate/edit.json', FILE_USE_INCLUDE_PATH);

//This works, but I want to use a relative path
$json=file_get_contents(dirname(dirname(__FILE__)).'/validate/edit.json');
How to&Answers:

Have you tried:

$json = file_get_contents(__DIR__ . '/../validate/edit.json');

__DIR__ is a useful magic constant.

For reasons why, see http://yagudaev.com/posts/resolving-php-relative-path-problem/.

When a PHP file includes another PHP file which itself includes yet another file — all being in separate directories — using relative paths to include them may raise a problem.

PHP will often report that it is unable to find the third file, but why?
Well the answer lies in the fact that when including files in PHP the interpreter tries to find the file in the current working directory.

In other words, if you run the script in a directory called A and you include a script that is found in directory B, then the relative path will be resolved relative to A when executing a script found in directory B.

So, if the script inside directory B includes another file that is in a different directory, the path will still be calculated relative to A not relative to B as you might expect.

Answer:

try using this

$json = file_get_contents("/path/to/your/file/edit.json", true);

As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include path search. This is not possible if strict typing is enabled since FILE_USE_INCLUDE_PATH is an int. Use TRUE instead.

Answer:

Of cause there is the possibility to use the include_path parameter. Set this parameter to ‘1’ then a search for the file in the include_path (in php.ini.) will be done. You have to do editing in the php.ini!

When using $json=file_get_contents(‘detail.php’); a call is done from a php file.
Use file_get_contents(‘detail.php’); to have the detail.php get executed. the file have to be in the root directory (same as the calling php file in which the file_get_contents() is situated). If detail.php is in a subdirectory i can not see any workaround than using the include_path parameter

Answer:

You can always try to do the opposite first to find out:

file_put_contents('FINDME', 'smth');
exit();
$json=file_get_contents('detail.php');
...

Then run something like this from your terminal on *nix system (or search for a file named FINDME using GUI on Windows)

find <root-dir-of-the-project> -name 'FINDME'

The command will output something like this:

<root-dir-of-the-project>/<directories>/FINDME

Now you know the root dir (from which the relative path is being taken) for the file where you are attempting to read the other files and you can construct the relative path easily