pâtissier
pâtissier

Reputation:

including files without having to specify $_SERVER['DOCUMENT_ROOT']

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?

Upvotes: 2

Views: 3457

Answers (3)

Narcissus
Narcissus

Reputation: 3194

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).

Upvotes: 3

Ryan Chouinard
Ryan Chouinard

Reputation: 2076

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.

Upvotes: 3

artlung
artlung

Reputation: 34013

You need the php.ini directive include_path

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

Upvotes: 5

Related Questions