Correct PHP way to check if external image exists?

后端 未结 9 1303
清歌不尽
清歌不尽 2021-02-04 08:49

I know that there are at least 10 the same questions with answers but none of them seems to work for me flawlessly. I\'m trying to check if internal or external image exists (is

9条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 09:37

    There are multiple steps, there is no single solution:

    1. Validate URL
    2. Check whether the file is available (can be done directly with step 3)
    3. Download the image into a tmp file.
    4. Use getimagesize to check the size of the image.

    For this kind of work you can catch the exceptions and handle them well to define your answer. In this case you could even suppress errors because it's intended that they trick might fail. So you handle the errors correctly.

    Because it's not possible to do a 100% check on it without having the actual image downloaded. So step 1 and 2 are required, 3 and 4 optional for a more definitive answer.

提交回复
热议问题