PHP to determine the existence of remote images, this method is also applicable to determine the existence of remote files, which is an efficient and accurate method, it is recommended to use this method, previously used Get_headers () method to determine that there is a problem:
functionCheck_remote_file_exists ($url) { $curl= Curl_init ($url); //do not retrieve datacurl_setopt ($curl, Curlopt_nobody,true); //Send Request $result= Curl_exec ($curl); $found=false; if($result!==false) { //Check if the HTTP response code is $statusCode= Curl_getinfo ($curl,Curlinfo_http_code); if($statusCode= = 200) { $found=true; }} curl_close ($curl); return $found;}
From the following example of invocation, the definition variable is assigned with a function return value:
// function Call: $exists = check_remote_file_exists (' http://www.baidu.com/img/baidu_sylogo1.gif '); if ($exists) { echo ' remote picture exists 'else { Echo ' The remote graph does not exist ';}
PHP to determine if a remote picture or file exists