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