Copy Code code as follows:
Determining 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 did not send a failure
if ($result!== false) {
and check to see if the HTTP response code is 200.
$statusCode = Curl_getinfo ($curl, Curlinfo_http_code);
if ($statusCode = = 200) {
$found = true;
}
}
Curl_close ($curl);
return $found;
}
Recently in a HTML5 music broadcast site, want to make my iphone and ipad cool, the front-end using jquery a plug-in jplayer, after the transformation effect is good.
The backstage uses PHP, collects the Baidu MP3 regularly. Take into account my server space chrysanthemum tight, of course, can only collect MP3 address, the file is not downloaded to the local. Considering Baidu MP3 path often change, it is egg pain, so must regularly judge the path of MP3 is right, so there is a PHP to determine whether the remote file exists this soft article. Starting with the Get_headers () method, I later heard that there was a problem with efficiency and didn't use this solution, but incidentally, here's a look at the effect of the Get_headers function:
Copy Code code as follows:
Default effect
Print_r (Get_headers ("yun_qi_img/baidu_sylogo1.gif"));
Results:
Array
(
[0] => http/1.1 OK
[1] => Date:thu, June 02:47:27 GMT
[2] => Server:apache
[3] => p3p:cp= "OTI DSP COR IVA 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 07:15:35 GMT
[6] => ETag: "65e-49a41e65933c0"
[7] => Accept-ranges:bytes
[8] => content-length:1630
[9] => cache-control:max-age=315360000
[A] => Expires:sun, may 2021 02:47:27 GMT
[One] => connection:close
[of] => content-type:image/gif
)
Effect of adding parameter 1
Print_r (Get_headers ("Yun_qi_img/baidu_sylogo1.gif", 1));
Results:
Array
(
[0] => http/1.1 OK
[Date] => Thu, 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, 07:15:35 GMT
[ETag] => "65E-49A41E65933C0"
[Accept-ranges] => bytes
[Content-length] => 1630
[Cache-control] => max-age=315360000
[Expires] => Sun, may 2021 02:49:28 GMT
[Connection] => Close
[Content-type] => image/gif
)
How, the Get_headers function is good, but since there is a problem of efficiency, it has to do not give priority to, curl is good, the following look at the practice of Curl
Copy Code code as follows:
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, plus only return the correct 200, reason unknown
//Send request
$result = Curl_exe C ($curl);
$found = false;
//If request did not send failed
if ($result!== false)
{
//re-check HTTP response code is
$statusCode = Curl_getinfo ($curl, C Urlinfo_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? ' exist ': ' not exist ';
$exists = check_remote_file_exists (' yun_qi_img/error.html ');
Echo $exists? ' exist ': ' not exist ';