Php filters nonexistent image resource _ php instances

Source: Internet
Author: User
This article summarizes several methods for filtering non-existent image resources using php, which is very simple and practical. For more information, see. Method 1:

The simplest method is to use fopen () to check whether the file can be opened. If the file can be opened, the file exists.

<?php$url = 'http://www.php.net/images/test.jpg';if( @fopen( $url, 'r' ) ) {  echo 'File Exits';} else { echo 'File Do Not Exits';}?>

Method 2:

/*** Filter nonexistent image resources ** @ author wanggeng
 
  
* @ Return vodi */private static function _ checkAll ($ url) {$ curl = curl_init ($ url); curl_setopt ($ curl, CURLOPT_NOBODY, true); $ result = false; $ res = curl_exec ($ curl); if ($ res! = False) {$ statusCode = curl_getinfo ($ curl, CURLINFO_HTTP_CODE); if ($ statusCode = 200) {$ result = true ;}} curl_close ($ curl ); return $ result ;}
 

First, create a curl link to the executed url, that is, the image or file link.
The initial variable is false.
Or the header information of the link. Each http request has an http Code.
We will verify it based on this code.
If the returned code is 200, it indicates that the resource exists and the previous variable has a value of true. Otherwise, no value is assigned.

Method 3:

CURL Method

CURL is a very useful class library. Let's take a look at how to use it to determine.

<? Php $ url2 = 'HTTP: // www.php.net/test.jpg'commandid ch = curl_init (); $ timeout = 10; curl_setopt ($ ch, CURLOPT_URL, $ url2); curl_setopt ($ ch, CURLOPT_HEADER, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout); $ contents = curl_exec ($ ch); // echo $ contents; if (preg_match ("/404/", $ contents) {echo 'file does not exist ';}?>

After curl_exec () is executed, if the file does not exist, the following information is returned:

HTTP/1.1 404 Not FoundDate: Tue, 14 Feb 2012 05:08:34 GMTServer: ApacheAccept-Ranges: bytesContent-Length: 354Content-Type: text/html

Use regular expressions to check whether 404 exists. If yes, the file does not exist.

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.