How can one check to see if a remote file exists using PHP?

前端 未结 22 2909
死守一世寂寞
死守一世寂寞 2020-11-22 05:52

The best I could find, an if fclose fopen type thing, makes the page load really slowly.

Basically what I\'m trying to do is

22条回答
  •  故里飘歌
    2020-11-22 06:39

    To check for the existence of images, exif_imagetype should be preferred over getimagesize, as it is much faster.

    To suppress the E_NOTICE, just prepend the error control operator (@).

    if (@exif_imagetype($filename)) {
      // Image exist
    }
    

    As a bonus, with the returned value (IMAGETYPE_XXX) from exif_imagetype we could also get the mime-type or file-extension with image_type_to_mime_type / image_type_to_extension.

提交回复
热议问题