Adcaelo
Adcaelo

Reputation: 19

PHP - chdir and require fail

I've a problem with a simple chdir and require.

From a first file : web/index_cluster.php

I'm trying to load a second one : ezpublish_legacy/index_cluster.php

My required file do not load but I've no clue why...


Here's my config.

There is nothing on ezpublish log and a Allowed memory size in apache

PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 240 bytes) in /path/to/my/www/web/index_cluster.php on line 11

Here's my (simplified) tree

www
    `-- ezpublish_legacy
        |-- index_cluster.php
    `-- web
        |-- index_cluster.php

Here's the original code

$legacyRoot = '/path/to/my/www/ezpublish_legacy';
chdir( $legacyRoot );
require 'index_cluster.php';

And here's my fix

$legacyRoot = '/path/to/my/www/ezpublish_legacy';
chdir( $legacyRoot );
require '/path/to/my/www/ezpublish_legacy/index_cluster.php';


I've tried everything I could think of :

$legacyRoot = '/path/to/my/www/ezpublish_legacy';
require $legacyRoot.'/index_cluster.php';

=> Working


$legacyRoot = '/path/to/my/www/ezpublish_legacy';
echo getcwd() . "\n";
chdir( $legacyRoot );
echo getcwd() . "\n";
die()
require 'index_cluster.php';

=> exactly what I'm expecting

/path/to/my/www/web
/path/to/my/www/ezpublish_legacy

Loading with absolute path and checking current directory on loaded file is giving the expected result

web/index_cluster.php

require '/path/to/my/www/ezpublish_legacy/index_cluster.php';

ezpublish_legacy/index_cluster.php

echo getcwd() . "\n";
die();

result in (what I'm expecting)

/path/to/my/www/web

web/index_cluster.php

$legacyRoot = '/path/to/my/www/ezpublish_legacy';
chdir( $legacyRoot );
require '/path/to/my/www/ezpublish_legacy/index_cluster.php';

ezpublish_legacy/index_cluster.php

echo getcwd() . "\n";
die();

result in (what I'm expecting)

/path/to/my/www/ezpublish_legacy

Update : I've tried something new :

require "/index_cluster.php" => instant fail

PHP Warning: require(/index_cluster.php): failed to open stream: No such file or directory in /path/to/my/www/web/index_cluster.php on line 11

require "index_cluster.php" => trying loading for 10 seconds then fail

PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 240 bytes) in /path/to/my/www/web/index_cluster.php on line 11


Upvotes: 1

Views: 653

Answers (2)

Adcaelo
Adcaelo

Reputation: 19

PHP configuration about "include_path" create a "loop" and a memory limit because my 2 files have the same name.

So web/index_cluster.php load indefinitely itself instead of loading ezpublish_legacy/index_cluster.php

which config exactly is not releavant to me because it's a iso-production config I cannot change.

Available solution (pick one)

  • Use absolute path for require
  • Rename 1 of the 2 files with another name
  • Search and correct your php config about include_path

Upvotes: 0

johannes
johannes

Reputation: 15989

An include or require which is not starting with ./, / or, on Windows, a drive letter is different from the others by using include_path. To solve your immediate issue you can use

require "./index_cluster.php"

to use a fixed relative path. You tried the absolute path /index_cluster.php which would look on filesystem root, not the actual location.

When not using this form but a plain

require "index_cluster.php"

this will search through the include_path. The include_path on my system looks like this:

$ php -r 'echo ini_get("include_path");'
.:/usr/share/php:/usr/share/pear

So PHP would look for these files in order:

./index_cluster.php
/usr/share/php/index_cluster.php
/usr/share/pear/index_cluster.php

Using include_path is bad because some systems are configured wrongly and miss . and this takes more time. Also, and that's what's hitting you, for processing a little bit more memory is required, that's why you're hitting the memory limit in this case. To fix this for longer term you should analyze the code, if you can reduce memory usage and if not probably increase the memory_limit in php.ini.

Upvotes: 1

Related Questions