developer tip

PHP에 is_file 또는 file_exists

optionbox 2020. 7. 27. 07:51
반응형

PHP에 is_file 또는 file_exists


파일이 지정된 위치 ($ path. $ file_name)에서 HDD에 있는지 확인해야합니다.

의 차이 무엇입니까 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 x 10000 1.5651218891144 초

    file_exists x 10000 1.5016479492188 초

    is_readable x 10000 3.7882499694824 초

  2. 파일 이 존재하지 않을:

    is_file x 10000 0.23920488357544 초

    file_exists x 10000 0.22103786468506 초

    is_readable x 10000 0.21929788589478 초

편집 : 이동 clearstatcache (); 루프 내부. CJ 데니스 감사합니다.

참고 URL : https://stackoverflow.com/questions/792899/is-file-or-file-exists-in-php

반응형