Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents is not finding a file that exists

I have a file that I'd like another script to access using file_get_contents

The file I'd like it to access is in the directory above it, so I'm using file_get_contents('../file.php?this=that')

However, it's returning No such file or directory and I can't understand why. The file is there.

I'm assuming it has something to do with it being a local file rather than a remote. Any ideas or workarounds?

like image 923
Rob Avatar asked Aug 25 '10 01:08

Rob


2 Answers

According to PHP.net the correct solution to reading files using file_get_contents function from the local server is using

// <= PHP 5
$file = file_get_contents('./people.txt', true);

// > PHP 5
$file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH);

Thought it might help instead of using workarounds!

like image 186
Schahriar SaffarShargh Avatar answered Oct 16 '22 07:10

Schahriar SaffarShargh


file_get_contents('../file.php?this=that')

This will never work, unless you build a full URL with the entire http://.... syntax. PHP will see this as a request to get a file named file.php?this=that one level above the current directory. It won't treat it as a relative URL and do an HTTP request, it'll use the local filesystem handler instead. You may very well have file.php up there, but since the local file system has no concept of URLs or query parameters, it won't know to strip off the ?this=that and just load file.php. Hence your 'no such file error'.

like image 29
Marc B Avatar answered Oct 16 '22 06:10

Marc B