Given a remote picture address (HTTP address) and then use Curl to request that the remote picture exists by checking the returned message header
But there's a serious efficiency problem here is the message body.
The body of the message returned the picture. There are settings that can only generate the message body so that the message body is empty or does not produce the message body?
//检查远程文件function checkRemoteFile($file_name, $path) { $path = $path . "/" . $file_name; $ch = curl_init(); $timeout = 30; //在尝试连接时等待的秒数。设置为0,则无限等待。 curl_setopt($ch, CURLOPT_URL, $path); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $contents = curl_exec($ch);//echo $contents; if (preg_match("/404/", $contents)) { echo '0'; return false; } elseif(preg_match("/200/", $contents)) { echo $contents; return true;
Reply content:
Given a remote picture address (HTTP address) and then use Curl to request that the remote picture exists by checking the returned message header
But there's a serious efficiency problem here is the message body.
The body of the message returned the picture. There are settings that can only generate the message body so that the message body is empty or does not produce the message body?
//检查远程文件function checkRemoteFile($file_name, $path) { $path = $path . "/" . $file_name; $ch = curl_init(); $timeout = 30; //在尝试连接时等待的秒数。设置为0,则无限等待。 curl_setopt($ch, CURLOPT_URL, $path); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $contents = curl_exec($ch);//echo $contents; if (preg_match("/404/", $contents)) { echo '0'; return false; } elseif(preg_match("/200/", $contents)) { echo $contents; return true;
No curl is OK, fopen seems to be able to open the remote file.
$hd = fopen($remoteUrl, 'r');if ($hd === false) die('404')else fclose($hd);
Oh, see your comment, with curl_setopt ($ch, Curlopt_nobody, true); Look
Isn't curl okay? You try this!
public function checkRemoteHttpFileExists($url) { $curl = curl_init($url); // 不取回数据 curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // @curl_setopt($curl, CUROPT_RETURNTRANSFER,1); // 发送请求 $result = @curl_exec($curl); $found = false; // 如果请求没有发送失败 if ($result !== false) { // 再检查http响应码是否为200 $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($statusCode == 200) { $found = true; } } curl_close($curl); return $found;}