Hongmu Notes
Home Language Notes PHP check if an image is a 404 error
Language Notes PHP

PHP check if an image is a 404 error

PHP check if an image is a 404 error

In PHP, you can determine whether an image corresponds to a 404 error by sending an HTTP request. Specifically, this can be achieved by using the `curl` library or the `file_get_contents` function to send an HTTP request and then checking whether the response status code is 404.

Here is an example of code using the `curl` library:

function is_image_404_using_curl($image_url) {
    $ch = curl_init($image_url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $status_code == 404;
}

In this function, we use the `curl_init` function to create a CURL handle, and then use the `curl_setopt` function to configure various parameters—such as disabling the output of the response body, not following redirects, and saving the response to a string. Next, we call the `curl_exec` function to send the HTTP request and use the `curl_getinfo` function to retrieve the response status code. Finally, we return the result based on whether the status code is 404.

Here is an example of code using the `file_get_contents` function:

function is_image_404_using_file_get_contents($image_url) {
    $headers = get_headers($image_url, 1);
    return strpos($headers[0], '404') !== false;
}

In this function, we use the `get_headers` function to retrieve the HTTP response headers and store the result in an associative array. Then, we check whether the status code in the response headers is 404 and return the corresponding result.

It is important to note that using this method to determine whether an image corresponds to a 404 error will trigger a single HTTP request; therefore, performance and network latency considerations should be taken into account. Additionally, some websites may prevent web crawlers from accessing their images; thus, in practical applications, it is essential to comply with relevant laws, regulations, and the specific terms and conditions of the target website.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 143
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 191
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Pipe unblocking and house leak repair website template 1011

Pipe unblocking and house leak repair website template 1011 Practical Collection Yiyou template

This set of eyoucms templates is suitable for the pipe unblocking and house leak repair industries. It has a professional design style and can display pipe unblocking services, leak repair projects, technical solutions and customer cases. Helps home maintenance companies display their brands online to attract home and business customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Summary of common problems in Yiyou CMS installation Yi...
👁 55
What exactly is the Huiyuan Authentication Function for?

What exactly is the Huiyuan Authentication Function for? Summary of pitfalls

Origin Authorization is a feature used within Content Delivery Networks (CDNs) to verify whether a request has the proper authorization to access the original content on the source server. When a user requests a particular resource, the CDN first checks whether this request requires Origin Authorization. The primary purpose of Origin Authorization is to prevent unauthorized users from directly accessing resources on the source server. It helps safeguard the source server against activities such as unauthorized linking, malicious downloading, and illegal distribution, thereby enhancing the security and integrity of the source server's content...
👁 273
Responsive decorative interior design website template – 0191

Responsive decorative interior design website template – 0191 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the interior decoration and renovation industry. Its modern, professional design style enables effective presentation of renovation design projects, construction techniques, material displays, and service workflows. It helps renovation and decoration companies attract homeowners online and secure new projects. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 43
(Adaptive Mobile Version) HTML5 Construction Company Website – PBootCMS Template; Responsive Construction Group Website Source Code Download – 0568

(Adaptive Mobile Version) HTML5 Construction Company Website – PBootCMS Template; Responsive Construction Group Website Source Code Download – 0568 Practical Collection pbootcms Template

An HTML5-responsive website template for construction companies using PbootCMS, compatible with both PC and WAP devices. Its professional and reliable design makes it ideal for construction groups or engineering firms to showcase their corporate strength and project portfolio. This template serves as a powerful tool for large construction enterprises to establish their brand image online. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 47
Industrial Electronic Components Service Provider Website Template 1012

Industrial Electronic Components Service Provider Website Template 1012 Practical Collection Yiyou template

An eYouCMS website template designed for industrial electronic component service providers, featuring a modern, tech-oriented design with clear information hierarchy. Ideal for showcasing electronic component products, technical support, and solutions – helping electronic component distributors or manufacturers build their online brand identity and expand their reach among industrial customers. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: eYouCMS Installation...
👁 35
Which is better: the Return Source Authentication feature or timestamp-based anti-link劫持 mechanism?

Which is better: the Return Source Authentication feature or timestamp-based anti-link劫持 mechanism? Summary of pitfalls

Origin-based authentication and timestamp-based anti-spoofing are both common technical measures used to protect resources against unauthorized access; each has its own advantages and disadvantages, and the appropriate solution can be selected based on specific requirements. Origin-based authentication: Advantages: This method determines whether to allow access to the original content on the source server by verifying the authentication information contained in the request. It offers relatively high flexibility, as different authentication rules can be configured according to specific requirements, enabling more granular access control. Disadvantages: Origin-based...
👁 210