2016-06-17 64 views
1

我有一個簡單的chdir和require的問題。PHP - chdir並要求失敗

從第一個文件:網絡/ index_cluster.php

我試圖加載第二個:ezpublish_legacy/index_cluster.php

我所需的文件不加載,但我不知道爲什麼...


這是我的配置。

  • PHP 5.4.16
  • 從升級的eZ發佈4.7升級到EZ Pubslih 5.90.0alpa1(基於SF2)
  • 紅帽企業Linux服務器版本6.4(聖地亞哥)

有沒事就ezpublish日誌和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 

這是我(簡化)樹

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

這裏的原代碼

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

這是我的固定

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


我用盡了一切我能想到的:

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

=>工作


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

=>正是我期待

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

加載以絕對路徑和檢查上加載文件當前目錄正在給期望的結果

網/ index_cluster.php

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

ezpublish_legacy/index_cluster。PHP

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

結果(我很期待)

/path/to/my/www/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(); 

結果(我很期待)

/path/to/my/www/ezpublish_legacy 

更新:我已經嘗試一些新的東西:

require "/index_cluster.php" => instant fail 

PHP的警告:要求(/ index_cluster .php):未能打開流:第11行/path/to/my/www/web/index_cluster.php中沒有此文件或目錄

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

PHP致命錯誤:用盡536870912個字節允許存儲器大小(試圖分配240個字節)/path/to/my/www/web/index_cluster.php第11行


+0

你錯誤消息是關於PHP內存限制。這不是關於chdir和require。 – Taras

+0

是真的,我第一次寫作時忘記了這個apache錯誤,因爲它對我來說並不「相關」......之後添加了它。儘管如此,問題仍然是由chdir/require組合 – Adcaelo

+0

*不相關,因爲我強制我的php內存限制到2G和更多相同的事情! PHP致命錯誤:允許的內存大小爲2147483648字節耗盡(試圖分配130968字節) – Adcaelo

回答

1
一種

includerequire,它不是以./,/開頭,或者在Windows上,通過使用include_path,驅動器號與其他驅動器號不同。要解決您的直接問題,您可以使用

require "./index_cluster.php" 

使用固定的相對路徑。您嘗試了絕對路徑/index_cluster.php,它將查看文件系統根目錄,而不是實際位置。

當不使用這種形式,而是一個普通的

require "index_cluster.php" 

這將通過include_path搜索。我的系統上的include_path看起來是這樣的:

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

所以PHP將查找這些文件依次是:

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

使用的include_path是不好的,因爲某些系統配置錯誤,錯過.,這需要更多的時間。而且,這就是你的命運,因爲需要處理更多內存,這就是爲什麼你在這種情況下達到內存限制的原因。要解決這個問題,您應該分析代碼,如果您可以減少內存使用量,並且可能不會增加中的memory_limit

+0

在我的衆多測試中丟失了我沒有想到的/index_cluster.php將檢查system_root不是文件夾,我正在尋找我的$ legacyRoot。 「/index_cluster.php」。 – Adcaelo

+0

需要「./index_cluster.php」**效果很好**,3人看着同一段代碼沒有想到這個......謝謝......但是需要「index_cluster.php」也應該工作,所以這是一個關於include_path的apache/php配置,它的限制太多了。 **我對嗎?** – Adcaelo

+0

不,不是include_path是問題。這是你的應用程序需要大量的內存,並接近極限。任何事情都可能達到極限。無論是優化應用程序或增加memory_limit,請參閱http://php.net/memory_limit – johannes

0

關於「include_path」的PHP配置會創建一個「循環」和內存限制,因爲我的兩個文件具有相同的名稱。

So web/index_cluster。PHP無限期地加載本身,而不是加載ezpublish_legacy/index_cluster.php

哪個配置完全不與我有關,因爲它是一個iso生產配置我無法更改。

可用的解決方案(任選其一)

  • 使用絕對路徑需要
  • 重命名2個文件的1與另一名
  • 搜索和糾正有關的include_path你的PHP配置