2

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.

2
  • no, subfolders are generally not included. did you use require ("utils/my_session.php"); in your original project? Commented Jun 3, 2011 at 12:27
  • Nope, that's the weird thing. I need to look at it again. I am missing something. Commented Jun 3, 2011 at 12:58

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The include for the sub-folder was indeed in the php.ini, but only for apache2. I had checked using php -i on the command line but this uses a different ini file.
Unfortunately sometimes the server configuration is set in a such way that command line execution of php and apache module are using different php.ini files. I had such problems in the past and I know this is very tricky and hard to catch. Happy to hear that you solved it. Regards.
2

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^^

Comments

0

Two solutions:

Add /usr/share/php/utils to your include_path.

or

Include your file with require ("utils/my_session.php");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.