2013-10-15 00:14
浏览 915

php filesize返回stat失败错误

I'm calling the following:

while ( (!file_exists('./download/ah141090676723_100.jpg')) || (filesize('./download/ah141090676723_100.jpg') == '1359') ) { code that retrieves a remote file and writes it to '/ah141090676723_100.jpg'  }

... and getting a "filesize(): stat failed for ./download/ah141090676723_100.jpg" error.

The problem I'm trying to solve is that the remote server is flaky, and sometimes returns a garbage response (which is always 1359 bytes long). So, I want to check to see if either A) the file doesn't exist (first run through), or B) the file equals garbage (1359); if either is true, attempt to grab and write the file. Rinse and repeat until we get something that's not garbage.

The code actually seems to be working -- the file is retrieved and written, and I haven't had any garbage responses get through this loop -- but the error mystifies me. I thought it might be that on the first run-through, the file doesn't exist, so filesize is throwing this error. But the "||" operator should be preventing that second evaluation on the first run-through... right?

I should mention that I'm calling "clearstatcache();" inside the loop, after the retrieval/write.

Any help appreciated!


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqian1517
    douqian1517 2016-01-12 20:47

    Change to

    while ( file_exists('./download/ah141090676723_100.jpg') && filesize('./download/ah141090676723_100.jpg') == 1359) 

    as file_exists is always required. filesize() returns "stat failed" when a file does not exist or is not readable.

    点赞 评论