In PHP scripts, whether calling include(), require(), fopen(), or your derivatives such as include_once, require_once, or even, move_uploaded_file(), one frequently runs right into an error or warning:

Failed to open up stream : No such file or directory.

You are watching: Failed to open stream: no such file or directory in

What is a good process come quickly find the root cause of the problem?


*

*

There are plenty of reasons why one could run right into this error and also thus a good checklist of what to check an initial helps considerably.

Let"s take into consideration that we space troubleshooting the following line:

require "/path/to/file"

Checklist

1. Check the paper path because that typos

either inspect manually (by visually check the path)

or move everything is referred to as by require* or include* to its very own variable, echo it, copy it, and try accessing the from a terminal:

$path = "/path/to/file";echo "Path : $path";require "$path";Then, in a terminal:

cat

2. Inspect that the file path is correct concerning relative vs absolute path considerations

if it is beginning by a forward slash "/" then it is no referring to the root of your website"s folder (the paper root), yet to the source of your server.for example, your website"s catalog might it is in /users/tony/htdocsif it is not beginning by a forward slash then that is one of two people relying ~ above the include path (see below) or the route is relative. If the is relative, then PHP will calculate reasonably to the path of the current working directory.thus, not loved one to the path of your net site"s root, or come the paper where you space typingfor that reason, constantly use absolute record paths

Best practices :

In order come make your script robust in case you relocate things around, while quiet generating an absolute course at runtime, you have 2 alternatives :

define a SITE_ROOT continuous yourself :

at the root of your net site"s directory, produce a file, e.g. Config.php

in config.php, write

define("SITE_ROOT", __DIR__);in every document where you want to referral the website root folder, incorporate config.php, and also then use the SITE_ROOT continuous wherever you prefer :

require_once __DIR__."/../config.php";...require_once SITE_ROOT."/other/file.php";These 2 practices additionally make your application an ext portable since it go not rely on ini settings like the incorporate path.

3. Check your include path

Another means to encompass files, neither fairly nor purely absolutely, is to count on the include path. This is frequently the case for libraries or frameworks such together the Zend framework.

Such one inclusion will certainly look like this :

include "Zend/Mail/Protocol/Imap.php"In the case, you will desire to make sure that the folder whereby "Zend" is, is component of the encompass path.

You can inspect the encompass path with :

echo get_include_path();You can include a folder to it with :

set_include_path(get_include_path().":"."/path/to/new/folder");

4. Examine that your server has access to that file

It might be the all together, the user to run the server process (Apache or PHP) simply doesn"t have permission to review from or create to that file.

To inspect under what user the server is running you have the right to use posix_getpwuid :

$user = posix_getpwuid(posix_geteuid());var_dump($user);To find out the permissions top top the file, type the adhering to command in the terminal:

ls -l and look in ~ permission symbolic notation

5. Inspect PHP settings

If nobody of the above worked, then the problem is most likely that some PHP settings forbid that to access that file.

Three settings could be relevant :

open_basedirIf this is set PHP won"t be able to access any document outside of the specified directory (not even through a symbolic link).However, the default behavior is for it no to be set in which instance there is no restrictionYou can readjust the setup either by editing and enhancing your php.ini file or her httpd.conf filesafe modeallow_url_fopen and also allow_url_includethis uses only to consisting of or opening files through a network procedure such as http:// not when trying come include papers on the local document systemthis deserve to be checked with ini_get("allow_url_include") and collection with ini_set("allow_url_include", "1")

Corner cases

If none of the above enabled to diagnose the problem, below are part special situations that might happen :

1. The consists of library relying top top the incorporate path

It can take place that you incorporate a library, because that example, the Zend framework, utilizing a loved one or pure path. For instance :

require "/usr/share/php/libzend-framework-php/Zend/Mail/Protocol/Imap.php"But then you still gain the very same kind that error.

This can happen because the file that you have (successfully) included, has actually itself an encompass statement for an additional file, and that 2nd include explain assumes that you have included the route of the library come the encompass path.

For example, the Zend framework record mentioned before could have the following encompass :

include "Zend/Mail/Protocol/Exception.php" which is no an inclusion by family member path, no one by absolute path. It is assuming that the Zend framework magazine has been added to the incorporate path.

In together a case, the only practical solution is to include the directory to your encompass path.

2. SELinux

If you space running Security-Enhanced Linux, then it might be the factor for the problem, by denying access to the file from the server.

To inspect whether SELinux is enabled on her system, run the sestatus command in a terminal. If the command does not exist, then SELinux is not on your system. If that does exist, then it need to tell you whether it is implemented or not.

To inspect whether SELinux policies are the reason because that the problem, friend can shot turning it off temporarily. But be CAREFUL, because this will disable defense entirely. Carry out not perform this top top your production server.

setenforce 0If friend no longer have the trouble with SELinux turn off, then this is the source cause.

To fix it, friend will have to configure SELinux accordingly.

The complying with context types will be necessary :

httpd_sys_content_t for files that you want your server to be able to readhttpd_sys_rw_content_t for papers on which you desire read and also write accesshttpd_log_t for log fileshttpd_cache_t for the cache directory

For example, to assign the httpd_sys_content_t context type to her website root directory, operation :

semanage fcontext -a -t httpd_sys_content_t "/path/to/root(/.*)?"restorecon -Rv /path/to/rootIf your document is in a house directory, girlfriend will additionally need to turn on the httpd_enable_homedirs boolean :

setsebool -P httpd_enable_homedirs 1In any kind of case, there might be a selection of factors why SELinux would deny accessibility to a file, relying on your policies. So friend will must enquire right into that. Here is a tutorial especially on configuring SELinux for a net server.

3. Symfony

If you are using Symfony, and also experiencing this error when uploading come a server, climate it can be that the app"s cache hasn"t to be reset, either since app/cache has been uploaded, or that cache hasn"t to be cleared.

You can test and also fix this by to run the adhering to console command:

cache:clear

4. Non ACSII characters inside Zip file

Apparently, this error deserve to happen likewise upon phone call zip->close() when some files inside the zip have actually non-ASCII personalities in their filename, such as "é".

See more: Oracion A San Charbel Para El Amor, Dinero, Trabajo, Salud, Oración A San Charbel Para Casos Difíciles

A potential equipment is come wrap the paper name in utf8_decode() before creating the target file.