Judging remote Files
- function Check_remote_file_exists ($url)
- {
- $curl = Curl_init ($url);
- Do not retrieve data
- curl_setopt ($curl, Curlopt_nobody, true);
- Send Request
- $result = curl_exec ($curl);
- $found = false;
- If the request does not fail to send
- if ($result!== false) {
- Check if the HTTP response code is 200
- $statusCode = Curl_getinfo ($curl, Curlinfo_http_code);
- if ($statusCode = = 200) {
- $found = true;
- }
- }
- Curl_close ($curl);
return $found;
- }
- ?>
Copy CodeRecently in a HTML5 music broadcast site, want to make my iphone and ipad cool, front-end with a jquery plug-in Jplayer, after the transformation of the effect is good. The backstage uses PHP, collects the MP3 of Baidu regularly. Considering I server space chrysanthemum tight, of course, can only collect MP3 address, the file is not downloaded to the local. Considering the Baidu MP3 path often change, it is the egg pain, so must be timed to determine the MP3 path is not right, so there is a PHP to determine whether the remote file exists this soft article. Start with the Get_headers () method, and later heard that there is an efficiency problem, so do not use this solution, but incidentally, let's look at the effect of the Get_headers function:
- Default effect
- Print_r (Get_headers ("http://www.baidu.com/img/baidu_sylogo1.gif"));
Copy CodeResults: Array ([0] = http/1.1 ok[1] [Date:thu], June 02:47:27 gmt[2] = server:apache[3] = p3p:cp= " OTI DSP COR IVA our IND COM "[4] = set-cookie:baiduid=7f6a5a2ed03878a7791c89c526966f3a:fg=1; Expires=fri, 01-jun-12 02:47:27 GMT; max-age=31536000; path=/; domain=.baidu.com; VERSION=1[5] = Last-modified:thu, Jan 07:15:35 gmt[6] = ETag: "65E-49A41E65933C0" [7] = = Accept-ranges: BYTES[8] = content-length:1630[9] [cache-control:max-age=315360000[10] = Expires:sun, 2021 02:47:2 7 gmt[11] = connection:close[12] = content-type:image/gif)
- The effect of adding parameter 1
- Print_r (Get_headers ("Http://www.baidu.com/img/baidu_sylogo1.gif", 1));
Copy CodeResults: Array ([0] = http/1.1 ok[date] = Thu, Geneva June (02:49:28 Gmt[server] = apache[p3p] = cp= "OTI DSP COR IVA our IND COM "[Set-cookie] = baiduid=4d875812fc482c0ade4f5c17068849ee:fg=1; Expires=fri, 01-jun-12 02:49:28 GMT; max-age=31536000; path=/; domain=.baidu.com; Version=1[last-modified] = Thu, Jan 07:15:35 Gmt[etag] = "65E-49A41E65933C0" [accept-ranges] = Bytes[c Ontent-length] = 1630[cache-control] [Max-age=315360000[expires] = Sun, May 2021 02:49:28 Gmt[connection] = Close[content-type] = image/gif) How, Get_headers function is still good, but since the efficiency has a problem, that had to not first consider, curl is good, below see curl Practice
-
- !--? php function check_remote_file_exists ($url)
- {
- $curl = Curl_init ($url );
- //Do not retrieve data
- curl_setopt ($curl, Curlopt_nobody, true);
- curl_setopt ($curl, Curlopt_customrequest, ' GET ');//Do not add this will return 403, add to return the correct 200, the reason is unknown
- //Send request
- $result = curl_exec ($curl);
- $found = false;
- //If the request does not fail to send
- if ($result!== false)
- {
- //re-check HTTP response code is up to $
- $statusCode = Curl_getinfo ($curl, curlinfo_http_code);
- if ($statusCode = =)
- {
- $found = true;
- }
- }
- curl_close ($curl);
- return $found;
- }
- $exists = check_remote_file_exists (' http://www.baidu.com/img/baidu_sylogo1.gif ');
- Echo $exists? ' Existence ': ' not present ';
- $exists = check_remote_file_exists (' http://www.baidu.com/test.jpg ');
- Echo $exists? ' Existence ': ' not present ';
- ;
Copy Code |