Rob
Rob

Reputation: 8101

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?

Upvotes: 9

Views: 41978

Answers (6)

Schahriar SaffarShargh
Schahriar SaffarShargh

Reputation: 1971

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!

Upvotes: 16

User1988
User1988

Reputation: 1997

use like this....

$string1=file_get_contents("c:/rose1/ram.txt"); echo $string1;

or $lines = file_get_contents('http://www.example.com/');

Upvotes: 3

Rob
Rob

Reputation: 8101

I went ahead and used a few $_SERVER variables, combining them together to get the full URL and used it in file_get_contents:

file_get_contents('http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/../file.php?this=that');

That did the trick. Thanks for the help everyone.

Upvotes: 4

Marc B
Marc B

Reputation: 360812

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

Upvotes: 25

Your Common Sense
Your Common Sense

Reputation: 157981

There is no file in that location.
You have to use proper path.

First of all do a echo getcwd(); to see what directory is current now (from which your relative path is built)
Then double check file.php location in relation to this directory.
Check filename case, it could be sensitive.

May I ask a reason why do you open a php file with this function?

Well, an answer:

$your_var = 1;
include('../file.php');

Upvotes: 4

Yanick Rochon
Yanick Rochon

Reputation: 53616

is getcwd() == dirname(__FILE__) ?

Once, I have encountered a problem where using a relative path was always throwing an error on some shared host. We ended up using absolute paths using dirname(__FILE__) as base path (setting a constant in the bootstrap and using that constant as base path value) and everything worked fine. We didn't dig further into the problem, but maybe you are encountering the same thing. I'm just guessing here.

Upvotes: 1

Related Questions