2

index.php以下のコードと(同じディレクトリ内の)別のファイルを含む2つのファイルで構成される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(ハンドラー内のもの)がファイルを見つけられないのはなぜですか?

4

3 に答える 3

3

理由は正確にはわかりませんが、PHPのドキュメントでは、次のようにregister_shutdown_function()記載されているメモでこれについて警告しています。

Note:

Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.

getcwd()あなたは実際に何が起こっているのかについての考えを得るためにエコーアウトを試みるかもしれません。

于 2012-12-18T00:27:29.097 に答える
2

関数のドキュメントを参照してください。

http://php.net/manual/en/function.register-shutdown-function.php

次のようなメモがあります。

Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.
于 2012-12-18T00:27:24.240 に答える
1

PHPの一部のSAPIでは、shutdown関数で作業ディレクトリが変更される可能性があります。register_shutdown_function:のマニュアルページにあるこのメモを参照してください。

スクリプトの作業ディレクトリは、Apacheなどの一部のWebサーバーのシャットダウン機能内で変更される可能性があります。

相対パスは作業ディレクトリによって異なります。変更すると、ファイルが見つかりなくなります。

代わりに絶対パスを使用する場合、その問題は発生しません。

$file = __DIR__ . '/' . 'example.png';

echo file_exists($file)
    ? 'outside the handler - exists'
    : 'outside the handler - does not exist';

$handle_shutdown = function() use ($file)
{
    echo file_exists($file)
        ? 'inside the handler - exists'
        : 'inside the handler - does not exist';
}

register_shutdown_function($handle_shutdown);
于 2012-12-18T00:26:32.760 に答える