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

5
  • you need to give absolute path like "images/example.jpg" Commented Aug 5, 2013 at 7:13
  • @MakC Did any of the answers helped? Commented Aug 5, 2013 at 10:11
  • @sankalpMishra yes i tried with below answers but due to allow_url_fopen it shows warnings on server SO then i have tried using CURL and it works for me. Commented Aug 5, 2013 at 11:15
  • @PravinS I can not use absolute path coz my image files are on another server and my code is on another server Commented Aug 5, 2013 at 11:16
  • @MakC use get_headers() function, it will return you length, http response and more Commented Aug 5, 2013 at 11:24

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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 ";
}

Comments

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

Comments

0

Use fopen function

if (@fopen($resultUF['destination'], "r")) {
    echo "File Exist";
} else {
    echo "File Not exist";
}

Comments

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.

Comments

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
   }

1 Comment

It is more recommended to use curl method instead of file_get_contents because the 2nd one is really slow compared to the curl. stackoverflow.com/questions/11064980/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.