How to fix failed to open stream warning?

A common warning when working with PHP can be:

Warning: include(myinclude.php): failed to open stream: No such file or directory in myfile.php on line 1

Why does this happen?

You have some lines to include a file like this:

include 'myinclude.php';

while your folder tree view is like this:



Now PHP can not find the file myinclude.php because it is not in the same directory as the index.php.

How to fix this?

A basic fix for this is setting the appropriate directory. For this example:

include 'includes/myinclude.php';

What if the included file is in a different subfolder then the including file?

Sometimes we have a file tree view like this:



in this case a simple

include '../includes/myinclude.php';

would do the trick, however the best practice for this case is to use script dir centric paths:

include __DIR__.'/../includes/myinclude.php';