I have a site where the PHP include path is /usr/share/php Within this path I have a sub-folder containing some utility files, e.g. /usr/share/php/utils. my_session.php is one of these utility files.
My application calls
require ("my_session.php")开发者_运维知识库;
and this works even though the file is actually within the utils folder.
I am trying to replicate this site in another installation and I am getting the error:
Failed opening required 'my_session.php' (include_path='.:/usr/share/php)
My question is:
Should the php include path also include files in sub-folders in the include path? This appears to be the case on my original site and I don't know why the behaviour seems to be different on the second site.
According to PHP documentation when you try to include a file, only paths listed in the include_path directive are checked. PHP is not supposed to check their subfolders.
My guess would be that this fails because you are using a relative path for the require
.
Your include_path
is defined as .:/usr/share/php
. That means only two folders will be checked when require('my_session.php')
gets executed:
- the current path
- the folder
/usr/share/php
I don't know your folder structure, so let's just imagine one:
my_project
- app
-- index.php
- lib
-- my_session.php
Now, if my_project/app/index.php
tries to require('my_session.php')
this will fail, because the current folder at the time executing the require
is my_project/app/
and there is no file entry of my_session.php
relative to my_project/app/
(it's relative to my_project/lib/
instead).
Long story short: Try to to use an absolute path instead of your relative one, e.g.
require('/var/www/html/my_project/lib/my_session.php');
Edit: removed and its subfolders, which was wrong. Too much __autoload in my brain^^
Two solutions:
Add /usr/share/php/utils
to your include_path.
or
Include your file with require ("utils/my_session.php");
精彩评论