views:

984

answers:

4

Hi...

Before the PHP Version update I used to be able to include files as following without specifying the document root:

<?php include '/absolute/path/to/files/file1.php'; ?>

However I now have to include the same file as following:

<?php include $_SERVER['DOCUMENT_ROOT'].'/absolute/path/to/files/file1.php'; ?>

What php.ini setting could have overridden the former behaviour?

+4  A: 

You need the php.ini directive include_path

See: http://us.php.net/manual/en/ini.core.php#ini.include-path

artlung
Here is my setting:include_path =.:/usr/share/pear:/usr/lib/php:/tmp
Thanks for the handy link.
+2  A: 

Including an absolute path should be working the same way straight through PHP 5.2.9 (haven't tried 5.3, but this shouldn't change). Since you're specifying an absolute path, the include_path directive has no bearing.

Can you provide some more information? What PHP version, platform, and the error you get back from include would be a great start.

Linux: RHEL 5 PHP: Version PHP 5.2.9 Error Messages I get are: PHP Warning: require(/conf/common.php): failed to open stream: No such file or directory in /var/www/vhosts/DOMAIN/httpdocs/tell-a-friend-fns.php on line 63 PHP Fatal error: require(): Failed opening required '/conf/common.php' (include_path='.:/usr/share/pear:/usr/lib/php:/tmp') in /var/www/vhosts/DOMAIN/httpdocs/tell-a-friend-fns.php on line 63

Okay, it looks like your application is living in /var/www/vhosts/DOMAIN, and you're looking for /conf/common.php, right? I don't know if your file is actually in /conf/ or if it's in /var/www/vhosts/DOMAIN/conf/ (I assume the latter, with the information given). If it's in /conf/, then make sure that your Web server user can read that directory. If not, change your include to /var/www/vhosts/DOMAIN/httpdocs/conf/common.php.

Better yet, you might be able to do include '../conf/common.php, depending on where common.php lives in relation to your main script for the requested page.

Remember that any path given with a leading "/" is absolute in relation to the file system, not the Web server document root. Any path given without a "/" is assumed to be a relative path, relative to your executing script (not the current file). My guess is that prepending $_SERVER['DOCUMENT_ROOT'] to your path is changing the absolute path to a relative path. I have no idea why an absolute path would act as a relative path pre-upgrade, unless you were operating in a jailed environment (common with virtual hosts) which got removed during the upgrade.

Ryan Chouinard
As you have guessed, I think when my hosting provider upgraded plesk /var/www/vhosts/DOMAIN/httpdocs portion was lost from the include_path. After appending it to the include_path everything is back to normal. Thanks!
+2  A: 

I always use something like:

require( dirname( __FILE__ ) . '/../../subdir/somefile.php' );

It gives you a relative path from the current file, but resolves to an absolute path (by using dirname on the current file).

Narcissus
Thanks for the handy tip.
A: 

Hi Narcissus, Thank you for your tip. I spent a few hours for solving the absolute path issue for my website. You are the guy. Thanks.

Kobyn