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

前端 未结 22 2911
死守一世寂寞
死守一世寂寞 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:24

    If you are dealing with images, use getimagesize. Unlike file_exists, this built-in function supports remote files. It will return an array that contains the image information (width, height, type..etc). All you have to do is to check the first element in the array (the width). use print_r to output the content of the array

    $imageArray = getimagesize("http://www.example.com/image.jpg");
    if($imageArray[0])
    {
        echo "it's an image and here is the image's info
    "; print_r($imageArray); } else { echo "invalid image"; }

提交回复
热议问题