In PHP scripts, whether calling
Failed to open stream : No such file or directory.
There are many reasons why one might run into this error and thus a good checklist of what to check first helps considerably.
Let's consider that we are troubleshooting the following line:
or move whatever is called by
include* to its own variable, echo it, copy it, and try accessing it from a terminal:
$path = "/path/to/file"; echo "Path : $path"; require "$path";
Then, in a terminal:
cat <file path pasted>
Best practices :
In order to make your script robust in case you move things around, while still generating an absolute path at runtime, you have 2 options :
require __DIR__ . "/relative/path/from/current/file". The
__DIR__magic constant returns the directory of the current file.
SITE_ROOT constant yourself :
in every file where you want to reference the site root folder, include
config.php, and then use the
SITE_ROOT constant wherever you like :
require_once __DIR__."/../config.php"; ... require_once SITE_ROOT."/other/file.php";
These 2 practices also make your application more portable because it does not rely on ini settings like the include path.
Another way to include files, neither relatively nor purely absolutely, is to rely on the include path. This is often the case for libraries or frameworks such as the Zend framework.
Such an inclusion will look like this :
In that case you will want to make sure that the folder where "Zend" is, is part of the include path.
You can check the include path with :
You can add a folder to it with :
It might be that altogether, the user running the server process (Apache or php) simply doesn't have permission to read from or write to that file.
To check under what user the server is running you can use posix_getpwuid :
$user = posix_getpwuid(posix_geteuid()); var_dump($user);
To find out the permissions on the file, type the following command in the terminal:
ls -l <path/to/file>
and look at permission symbolic notation
If none of the above worked, then the issue is probably that some PHP settings forbid it to access that file.
Three settings could be relevant :
phpinfo()or by using
ini_get("allow_url_include")and set with
If none of the above enabled to diagnose the problem, here are some special situations that could happen :
It can happen that you include a library, for example the Zend framework, using a relative or absolute path. For example :
But then you still get the same kind of error.
This could happen because the file that you have (successfully) included, has itself an include statement for another file, and that second include statement assumes that you have added the path of that library to the include path.
For example, the Zend framework file mentioned before could have the following include :
which is neither an inclusion by relative path, nor by absolute path. It is assuming that the Zend framework directory has been added to the include path.
In such a case, the only practical solution is to add the directory to your include path.
If you are running Security-Enhanced Linux, then it might be the reason for the problem, by denying access to the file from the server.
To check whether SELinux is enabled on your system, run the
sestatus command in a terminal. If the command does not exist, then SELinux is not on your system. If it does exist, then it should tell you whether it is enforced or not.
To check whether SELinux policies are the reason for the problem, you can try turning it off temporarily. However be CAREFUL, since this will disable protection entirely. Do not do this on your production server.
If you no longer have the problem with SELinux turned off, then this is the root cause.
To solve it, you will have to configure SELinux accordingly.
The following context types will be necessary :
httpd_sys_content_tfor files that you want your server to be able to read
httpd_sys_rw_content_tfor files on which you want read and write access
httpd_log_tfor log files
httpd_log_tfor the cache directory
For example, to assign the
httpd_sys_content_t context type to your website root directory, run :
semanage fcontext -a -t http_sys_content_t "/path/to/root(/.*)?" restorecon -Rv /path/to/root
If your file is in a home directory, you will also need to turn on the
httpd_enable_homedirs boolean :
setsebool -P httpd_enable_homedirs 1
In any case, there could be a variety of reasons why SELinux would deny access to a file, depending on your policies. So you will need to enquire into that. Here is a tutorial specifically on configuring SELinux for a web server.
If you are using Symfony, and experiencing this error when uploading to a server, then it can be that the app's cache hasn't been reset, either because
app/cache has been uploaded, or that cache hasn't been cleared.
You can test and fix this by running the following console command: