Reputation: 268
Here's my code:
<?php
$file = 'install.php';
echo file_get_contents($file);
?>
This code is actually inside the file being passed into file_get_contents(). Why am I getting a "failed to open stream: No such file or directory in /path/to/install.php" warning? It doesn't make sense, because the exact same thing works perfectly in another project I have on the same server.
Please help!
BTW I have cURL enabled, and allow_fopen is set to On. I have no idea what the problem could be.
UPDATE:
It works as expected when I use an absolute path (/var/www/path/to/install.php) but, as the purpose of this is to set up an auto-installation script, I really need to be able to use a relative path. Is there ANY where to achieve this?
Upvotes: 0
Views: 447
Reputation: 56557
use:
echo htmlspecialchars(file_get_contents($file));
The problem was the fact, that the source of that file contained <?php
and ?>
tags, so, they are outputed well, but your browser doesnt display the block visually (View Page's source, and you will see there.)
Upvotes: 0
Reputation: 3280
Since you said this code is actually inside install.php
(if I understood correctly) doing the following would be sufficient:
<?php
echo file_get_contents(__FILE__);
?>
Also, it would make your script not only filesystem-independent but also filename-independent.
Upvotes: 0
Reputation: 9857
The error is due to the being unreadable. This is normally due to it being non-existent (wrong file path) or the current user (e.g www-data
) having incorrect file permissions.
You can check this, to avoid the error:
$file = realpath('foo.php');
if (! file_exists($file)) {
throw new \Exception(sprintf('The file %s cannot be found', $file));
} else if (! is_readable($file)) {
throw new \Exception(sprintf('The file %s cannot be read', $file));
} else {
$contents = file_get_contents($file);
}
Check out the documentation on file system functions for more helpful ways to debug these kind of errors.
Update
To expand the relative file path, use realpath()
Upvotes: 1
Reputation: 8769
If I were you, I do two checks up:
I. is install.php located in the same directory as the script file containing:
<?php
$file = 'install.php';
echo file_get_contents($file);
?>
II. The Unix like owner of the the install.php is the web page caller, for example, in Debian OS, the group and owner of install.php shall be www-data ? If the owner is root, the web page can not read this file because of the file protection from reading.
This error can be made as you login as root, you copy a whole directory under the root user, root will be the owner of his creation. No other user can read those directories/files.
Upvotes: 0