Php filters nonexistent image resources _ PHP Tutorial

Source: Internet
Author: User
Php filters nonexistent image resources. This article summarizes several methods for filtering nonexistent image resources using php. it is very simple and practical. For more information, see. Php filters nonexistent image resources

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

$ Url = 'http: // www.jb51.net/images/test.jpg ';

If (@ fopen ($ url, 'r '))

{

Echo 'file exists ';

}

Else

{

Echo 'file Do Not Exits ';

}

?>

Method 2:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/**

* 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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

$ Url2 = 'http: // www.jb51.net/test.jpg ';

$ 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:

?

1

2

3

4

5

6

HTTP/1.1 404 Not Found

Date: Tue, 14 Feb 2012 05:08:34 GMT

Server: Apache

Accept-Ranges: bytes

Content-Length: 354

Content-Type: text/html

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

This article summarizes several methods for filtering non-existent image resources using php. it is very simple and practical. For more information, see ....

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.