添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

Scott

that does appear to be the root of the problem; I noticed that when the file already exists, the error isn't thrown. So when the file doesn't exist, it's "not writeable". But the question still remains: shouldn't "||" prevent it from getting to that part of the code? – FoulFoot Oct 15, 2013 at 1:24 All these years later, I think I understand what's going on. Sorry for thread necro. I always thought that PHP would step through the script in logical fashion, and only stop if it hit an error -- which it wouldn't, under this theory, because the double-OR operator ("||") prevents it from getting to filesize() if file_exists() fails. But that's not how PHP works. At runtime, it parses the entire script first, and then executes it. That's why a misplaced comma in a section of your script that is otherwise unreachable will terminate the script in an error. :) – FoulFoot Aug 18, 2020 at 15: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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.