file_get_contents is not finding a file that exists

Rob picture Rob · Aug 25, 2010 · Viewed 37k times · Source

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?

Answer

Marc B picture Marc B · Aug 25, 2010
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'.