Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Installed apache and PHP on a new server. Using include("blah.php"); works fine but it doesn't work when I try to include from the directory behind, include("../blah.php");

PHP.ini has both: allow_url_fopen = On allow_url_include = On

Does anyone know what is causing this?

share|improve this question

5 Answers 5

According to the documentation

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

When you include ("blah.php"); PHP uses the include_path, but if you use a relative specifier, like . or .., PHP builds a path relative to where it is, i.e. the currently executing script.

share|improve this answer

Is it a Windows installation? If yes, you are using the wrong directory separator. You can avoid such discrepancies by using the DIRECTORY_SEPARATOR constant:

include('..'.DIRECTORY_SEPARATOR.'blah.php');

You can also use absolute paths instead:

include dirname(realpath(__FILE__)).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'blah.php'
share|improve this answer
1  
Thanks, but it's CentOS. –  DRKM Oct 26 '10 at 15:09
1  
All Windows versions ­— and even MS-DOS starting with 2.0 — accept / as a directory separator. –  grawity Oct 26 '10 at 18:43

Try including the full path to the file (e.g. /path/to/your/file) to see if its a context problem, you may be in the wrong directory. is_readable() is not the greatest function, try using file_exists to check if the file is there first. Also double-check the file permissions just in case.

share|improve this answer

Try using something like this:

include(dirname(__FILE__).'/../file.php')

The issue with relative includes is if you are not in the working directory you expect when you execute the file, they will fail. Remember, they are relative to getcwd(), not the location of the file. Using the dirname(FILE) trick means they end up relative to their actual location on disk.

Note that if you are using 5.3.x or above, you can use DIR instead of dirname(FILE)

share|improve this answer

Check your logs (or put error_reporting(E_ALL)). If a file is unreadable, or not loaded, you will see a log entry with an explanation. Paste it here if it doesn't help you.

share|improve this answer
    
Ah thanks, I can now atleast see an error (I wasn't getting any before. Does this mean aything to you because I have no idea why it can't read the file: [Tue Oct 26 15:43:49 2010] [error] [client 84.xx.xx.xx] PHP Warning: include() [<a href='function.include'>function.include</a>]: Failed opening '../panel.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/t/panel.php on line 18 –  DRKM Oct 26 '10 at 14:59
    
I've just noticed that if I access the file I'm trying to include directly, no problems. Only has problems accessing it when using include. –  DRKM Oct 26 '10 at 15:01
    
I've also just done a if (is_readable("/t/panel.php") and it's coming back not readable.. Why is it readable directly through the browser, but not through include? –  DRKM Oct 26 '10 at 15:17
    
Check in the main script the actual working directory with 'echo getcwd();' just before the include, to be sure that you are in the right one. Maybe the script change the CWD to something else. –  Dom Oct 27 '10 at 6:41
    
You can try to put temporaly the path in absolute to be sure that your file can be included –  Dom Oct 27 '10 at 6:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.