Several methods for PHP to determine whether a remote file exists

Source: Internet
Author: User
When you make a picture preview, you may encounter a problem: to determine whether a remote file exists (not the same server ). Code: 0102030405060708091011121314151617181920212223242526272829303132333435 // method 1 functionfile_exi... "/> <scriptt encountered a problem in making an image preview, that is, to determine whether a remote file exists (not the same server ). The code is as follows: 0102030405060708091011121314151617181920212223242526272829303132333435 // method 1 function file_exists ($ url) {$ ch = curl_init (); curl_setopt ($ ch, curlopt_url, $ url); curl_setopt ($ ch, curlopt_nobody, 1); // do not download curl_setopt ($ ch, curlopt_failonerror, 1); curl_setopt ($ ch, curlopt_returntransfer, 1); if (curl_exec ($ ch )! = False) return true; elsereturn false;} // method 2 function file_exists2 ($ url) {if (file_get_contents ($ url, 0, null, 0, 1) return 1; elsereturn 0;} // method 3 function 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; // if the request fails to be sent, if ($ result! = False) {// check whether the http response code is 200}. Method 1 returns FALSE regardless of whether the image is present. Method 2 is feasible in windows, in LINUX, TRUE is returned regardless of the image. Method 3 should be the most appropriate. In addition, the get_headers () method has an efficiency problem. we recommend that you do not use this solution for fsockopen: 01020304050607080910111213141516171819 $ url =" http://www.baidu.com/img/baidu_sylogo1.gif "; $ Info = parse_url ($ url); $ fp = fsockopen ($ info ['host'], 80, $ errno, $ errstr, 30); fputs ($ fp, "GET {$ info ['path']} HTTP/1.1 \ r \ n"); fputs ($ fp, "Host: {$ info ['host']} \ r \ n "); fputs ($ fp," Connection: close \ r \ n "); $ headers = array (); while (! Feof ($ fp) {$ line = fgets ($ fp); if ($ line! = "\ R \ n") {$ headers [] = $ line;} else {break;} echo"
"; Print_r ($ headers); the http status code is used to determine whether a file exists. for example, if the response is 302,301,404 or not, the file exists. Fopen (): 010203040506070809101112
 CURL: 01020304050607080910111213141516
 

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.