Share icon

Hey, we can see many website have default images if image is broken, so question is how to detect it using php the image is not found given url or not, in php you can do this by just do simple script using cRUL.

cURL is a library that lets you make HTTP requests in PHP, with the script you can check the http request to access different url.

You can use the below code to use this 

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$image_url = "https://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x.gif"; // Found
$image_url = "https://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x%20404.gif"; // Not Found
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if($httpCode != 404 ){
   echo "Found";
}else{
   echo "Not Found";
}

In above example there are $image_url variable with known and unknown image you can test it by commenting the next one.

hopes this simple script with help you solve the broken image.

thanks

Comments

Profile picture for user Duan Camargo
Anonymous (not verified)
Sun, 07/11/2021 - 08:40
Profile picture for user Duan Camargo
halliemichaelis66 (not verified)
Sun, 10/31/2021 - 07:37
Profile picture for user Duan Camargo
eulahb25824911… (not verified)
Sun, 10/31/2021 - 07:42
Profile picture for user Duan Camargo
zoracallahan32956 (not verified)
Sun, 10/31/2021 - 07:52
Profile picture for user Duan Camargo
ntccecile9795214663 (not verified)
Sun, 10/31/2021 - 08:04
Profile picture for user Duan Camargo
francesca23118747 (not verified)
Sun, 10/31/2021 - 08:20
Profile picture for user Duan Camargo
joeylemay06775999874 (not verified)
Sun, 10/31/2021 - 08:37
Profile picture for user Duan Camargo
tyrone51a81594699 (not verified)
Sun, 10/31/2021 - 08:48
Profile picture for user Duan Camargo
petraditter4899100 (not verified)
Sun, 10/31/2021 - 08:55

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.