Implementation code using Curl to determine if a remote file exists under PHP

Source: Internet
Author: User
Tags ranges
The code is as follows:
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;
}

Recently 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:
Copy the code code as follows:
Default effect
Print_r (Get_headers ("http://www.baidu.com/img/baidu_sylogo1.gif"));
Results:
Array
(
[0] = = http/1.1-OK
[1] = Date:thu, 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
[Ten] = Expires:sun, 2021 02:47:27 GMT
[One] = Connection:close
[[] = Content-type:image/gif
)
The effect of adding parameter 1
Print_r (Get_headers ("Http://www.baidu.com/img/baidu_sylogo1.gif", 1));
Results:
Array
(
[0] = = http/1.1-OK
[Date] = Thu, 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
[Content-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
Copy the 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 '); No, this will return 403, plus it returns the correct 200 for an unknown reason.
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;
}
$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 ';
  • 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.