Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents with relative path

Tags:

php

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'); 
like image 592
user1032531 Avatar asked Apr 22 '15 13:04

user1032531


People also ask

Which is faster cURL or file_get_contents?

This is old topic but on my last test on one my API, cURL is faster and more stable. Sometimes file_get_contents on larger request need over 5 seconds when cURL need only from 1.4 to 1.9 seconds what is double faster.

What is the difference between file_get_contents () function and file () function?

The file_get_contents() function reads a file into a string. The file_put_contents() function writes data to a file.

What will the file_get_contents () return?

The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is the function file_get_contents () useful for?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string.


2 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.

like image 186
Samuel Parkinson Avatar answered Oct 05 '22 18:10

Samuel Parkinson


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.

like image 34
Hara Prasad Avatar answered Oct 05 '22 20:10

Hara Prasad