0

I run into a very weird situation with file_exists function. The hosting company said their php was configured in CGI mode instead of PHP mode. below is the code. It checks the existence of the file called test.txt in data folder on the fly during 50 seconds or so when loading the page containing the code. If file found, display "File exists" and exits the while loop. If no file found in 50 seconds, display "File does not exist" and breaks the loop too finishing loading the page.

Strange thing 1: it was not working as expected, can find the file only first time loading the page when file is there. It continues displaying "File exists" even after test.txt got removed when I refresh the page. If test.txt is not in the data folder at all, it displays "file not exists" even after I move back test.txt in the folder.

Strange thing 2: If I put a bigger file say over 170K in size, it looks working well, small files not though, especially under 40 bytes. I have tried many different ways to check file existence including absolute path, still no luck.

Thanks for any clue!

loading page...

$counter= 1;

while ($counter++) {

    sleep(5);

    if (file_exists("data/test.txt")) {
    echo "File exists";
    break;
    }

    if ($counter>10){
    echo "File does not exist";
    break;
    }

}
4

2 回答 2

2

PHP caches the results. Use clearstatcache(); before you use file_exists().

于 2013-02-21T21:47:02.203 回答
0

由于您在循环中多次检查此文件的存在,因此您可能需要在此处考虑缓存问题。

取自- _ _file_Exists()

注意:此函数的结果被缓存。有关详细信息,请参阅clearstatcache()

也许你应该尝试将你的脚本修改成这样 -

while ($counter++) {
  sleep(5);
  clearstatcache(); 
  if (file_exists("data/test.txt")) {
    echo "File exists";
    break;
  }
  ...
}
于 2013-02-21T21:52:46.917 回答