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');

$image_url = ""; // Found
$image_url = ""; // Not Found
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_NOBODY, true);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if($httpCode != 404 ){
   echo "Found";
   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.


Add new comment

Restricted HTML