創建一個包括2個文件PHP項目 - index.php
包含下面的代碼和其他文件(在相同的目錄)稱爲example.png
。關機處理程序和相對路徑
echo file_exists('example.png')
? 'outside the handler - exists'
: 'outside the handler - does not exist';
register_shutdown_function('handle_shutdown');
function handle_shutdown()
{
echo file_exists('example.png')
? 'inside the handler - exists'
: 'inside the handler - does not exist';
}
foo();
運行index.php
。
這裏就是你會得到什麼:
outside the handler - exists
Fatal error: Call to undefined function foo() in /path/to/project/index.php on line 16
inside the handler - does not exist
這裏是我的問題。
爲什麼不能內部file_exists
(一個在處理程序),找到該文件?
example.php or example.png? – rendon
這是衆所周知的不起作用。您需要在PHP的關閉階段使用絕對路徑。請參閱http://php.net/register_shutdown_function上的註釋 – hakre
這可能是一個錯字,但是從輸出中可以看到,同一個文件存在檢查返回handle_shutdown函數內外的不同值 –