0

I am trying to check whether image file is exist on server Or not. I am getting image path with other server.

Please check below code which I've tried,

$urlCheck = getimagesize($resultUF['destination']);

if (!is_array($urlCheck)) {
  $resultUF['destination'] = NULL;
}

But, it shows below warning

Warning: getimagesize(http://www.example.com/example.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in

Is there any way to do so?

Thanks

Manish Chauhan
  • 595
  • 2
  • 7
  • 14

6 Answers6

3
$url = 'http://www.example.com/example.jpg)';
print_r(get_headers($url));

It will give an array. Now you can check the response to see if image exists or not

Engineer
  • 5,911
  • 4
  • 31
  • 58
1

You need to check that the file is exist regularly on server or not.you should used:

is_file .For example

$url="http://www.example.com/example.jpg";

if(is_file($url))
{
echo "file exists on server";
}
else
{
echo "file not exists on server ";
}
Harshal
  • 3,562
  • 9
  • 36
  • 65
1

Fastest & efficient Solution for broken or not found images link
i recommend you that don't use getimagesize() because it will 1st download image then it will check images size+if this will not image then it will throw exception so use below code

if(checkRemoteFile($imgurl))
{
//found url, its mean
echo "this is image";
}

function checkRemoteFile($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    // don't download content
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(curl_exec($ch)!==FALSE)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Note: this current code help you to identify broken or not found url image this will not help you to identify image type or headers

Hassan Saeed
  • 6,326
  • 1
  • 39
  • 37
0

Issue is that the image may not exist or you don't have a direct permission for accessing the image, else you must be pointing an invalid location to the image.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

you can use file_get_contents. This will cause php to issue a warning along side of returning false. You may need to handle such warning display to ensure the user interface doesn't get mixed up with it.

 if (file_get_contents($url) === false) {
       //image not foud
   }
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • It is more recommended to use curl method instead of file_get_contents because the 2nd one is really slow compared to the curl. https://stackoverflow.com/questions/11064980/php-curl-vs-file-get-contents – Eryk Wróbel Aug 13 '19 at 06:29
0

Use fopen function

if (@fopen($resultUF['destination'], "r")) {
    echo "File Exist";
} else {
    echo "File Not exist";
}
Bora
  • 10,529
  • 5
  • 43
  • 73