Denis
Denis

Reputation: 359

PHP - same path, but require gets different file (localhost vs server)

I am currently working on a poorly written legacy web project yet still trying to understand certain weird things about it. Let's say I have the following file structure

- htdocs/
-- index.php
-- vendor/
--- autoload.php
-- src/
--- news2.php
--- vendor/
---- autoload.php

The htdocs/index.php file includes htdocs/src/news2.php file. And the htdocs/src/news2.php includes the line

require 'vendor/autoload.php';

The local XAMPP copy works as expected and includes htdocs/src/vendor/autoload.php However, the server version behaves differently and includes htdocs/vendor/autoload.php instead. Is that normal behaviour and what could be the reason? I checked if there are any symlinks and there are none. Otherwise, I am clueless. PHP is 5.6, the server has more or less regular Ubuntu LAMP.

Upvotes: 1

Views: 810

Answers (1)

cn7r66
cn7r66

Reputation: 184

PHP include path are resolved based on the include_path value in the php.ini configuration file.

include_path value is a list of directories separated by :

The default value of include_path is something like ".:/usr/share/php"

In this case PHP will look first in the current working dir (.) then in /usr/share/php.

If the file you want to include is not found in any of this directories it will look in the directory of the script where the require resides.

Probably you don't have the dot in front of the include_path setting in your local php.ini and the include path is always resolved relatively to the script directory.

You can remove the dot in the include_path setting on your server or change the line in news2.php to require __DIR__ . /vendor/autoload.php

You can also use get_include_path to check your current configuration and set_include_path function to change the include_path setting programmatically without edit the php.ini

From the PHP Manual:

https://secure.php.net/manual/en/function.include.php

https://secure.php.net/manual/en/ini.core.php#ini.include-path

https://secure.php.net/manual/en/function.set-include-path.php

https://secure.php.net/manual/en/function.get-include-path.php


Edit based on comments:

Under XAMPP for Windows the default value for include_path is C:\path_to_xampp\php\PEAR to keep consistency with Linux's default you need to add .

Under Windows you have to use semicolon (;) instead of colon (:) for separating multiple paths. Anyhow, if multiple path are used, you must surround everything in quotes:

include_path= ".;C:path_to_xampp\php\PEAR"

If this still doesn't fix the issue keep in mind that Apache's configuration can overwrite any setting with php_value.

You can look for php_value occurrences in httpd.conf and .htaccess

From the PHP Manual:

https://secure.php.net/manual/en/configuration.changes.php

Upvotes: 2

Related Questions