Php determines whether remote files are available in advance _ PHP Tutorial

Source: Internet
Author: User
Php checks whether remote files are available in advance. We use functions related to phpcurl to access remote files, and then determine whether the files can be normally used based on the returned status, if you have any need, you can refer to the following code to copy and use php curl functions to access remote files. then, you can determine whether the files can be used normally based on the returned status, for more information, see
The code is as follows:

// Determine the remote file
Function check_remote_file_exists ($ url)
{
$ Curl = curl_init ($ url );
// Do not retrieve data
Curl_setopt ($ curl, CURLOPT_NOBODY, true );
// Send the request
$ Result = curl_exec ($ curl );
$ Found = false;
// Failed to send the request
If ($ result! = False ){
// Check whether the http response code is 200.
$ StatusCode = curl_getinfo ($ curl, CURLINFO_HTTP_CODE );
If ($ statusCode = 200 ){
$ Found = true;
}
}
Curl_close ($ curl );

Return $ found;
}

Method 2

The code is as follows:
$ Url = "/upload/201110/20111008192257383 .gif ";
$ Array = get_headers ($ url, 1 );
If (preg_match ('/ 200/', $ array [0]) {
Echo"
";
Print_r ($ array );
} Else {
Echo "invalid url resource! ";
}

Curl-related functions to access remote files, and then determine whether the files can be normally used based on the returned status. if you need them, refer to the following code to copy the files...

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.