質問

私はチェックをする必要の場合ファイルをハードディスクで指定された位置($path.$file_name).

であるとの違い is_file()file_exists() 機能だけが上がるようになるでしょう/高速で使用PHP?

役に立ちましたか?

解決

与えられたパスがディレクトリを指す場合は、

is_file()falseを返します。指定されたパスが有効なファイルまたはのディレクトリを指す場合file_exists()trueを返します。だから、あなたのニーズに完全に依存することになります。あなたが知りたい場合は、の特別のそれはファイルかどうか、使用is_file()だ場合。それ以外の場合は、file_exists()を使用します。

他のヒント

is_file() は早いですが、最近のベンチマークする file_exists() 若干速くした。ねによって異なります。

私の試験ベンチマーク:

benchmark('is_file');
benchmark('file_exists');
benchmark('is_readable');

function benchmark($funcName) {
    $numCycles = 10000;
    $time_start = microtime(true);
    for ($i = 0; $i < $numCycles; $i++) {
        clearstatcache();
        $funcName('path/to/file.php'); // or 'path/to/file.php' instead of __FILE__
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "$funcName x $numCycles $time seconds <br>\n";
}

編集:@Tivieのコメントとなります。変数のサイクルから1000 10k.その結果:

  1. 時のファイル が存在する:

    is_file x10000 1.5651218891144秒

    file_exists x10000 1.5016479492188秒

    is_readable x10000 3.7882499694824秒

  2. 時のファイル 存在しない:

    is_file x10000 0.23920488357544秒

    file_exists x10000 0.22103786468506秒

    is_readable x10000 0.21929788589478秒

編集:移動clearstatcache();内側のループを実行します。コCJ Dennis.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top