This article summed up a few use PHP to filter the existence of the image of the resources of the method, very simple and practical, the need for small partners can refer to.
Method One:
The simplest way is to use fopen (), to see if the file can be opened, can play on the file of course there.
?
| 1 2 3 4 5 6 7 8 9 10 11-12 |
<?php $url = ' http://www.jb51.net/images/test.jpg '; if (@fopen ($url, ' R ')) {echo ' File Exits ';} else {echo ' File does not Exits ';}?> |
Method Two:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * filter image resources that do not exist * & nbsp; * @author Wanggeng <wanggeng123@vip.qq.com> * @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 = = { $result = True; } } curl _close ($curl); return $result; } |
First, create a curl link to the execution URL, which is the link to the picture or file.
The initial variable is false
or open a linked Head header information each HTTP request will have an HTTP Code
We're going to verify this code.
If the return code is 200 to prove that the resource exists to the previous variable a value of true otherwise it will not be assigned a value
Method Three:
CURL method
Curl is a very useful class library, see below How to use it to judge.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
<?php $url 2 = ' http://www.jb51.net/test.jpg '; $ch = Curl_init (); $timeout = 10; curl_setopt ($ch, Curlopt_url, $url 2); 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 ';}?> |
Curl_exec () If the file does not exist after execution, the following information is returned:
?
| 1 2 3 4 5 6 |
http/1.1 404 Not Found date:tue Feb 05:08:34 GMT Server:apache -type:text/html |
Use the positive to see if there are 404, some words file does not exist.