Home >Backend Development >PHP Tutorial >curl - PHP How to quickly determine whether a remote file exists
My code
:
<code class="php">//判断远程文件是否存在 if(! function_exists('check_url')) { function check_url($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $res = curl_exec($ch); if ($res !== false){ $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); return $statusCode; } curl_close($ch); return false; } }</code>
This unified path is not very stable. Sometimes it succeeds and sometimes it fails, and the waiting time is too long!
My code
:
<code class="php">//判断远程文件是否存在 if(! function_exists('check_url')) { function check_url($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $res = curl_exec($ch); if ($res !== false){ $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); return $statusCode; } curl_close($ch); return false; } }</code>
This unified path is not very stable. Sometimes it succeeds and sometimes it fails, and the waiting time is too long!
Strange requirement, if the remote server is your own, then just write an interface on the server to check whether the file exists. If it is not yours, you can only check whether your network and the server-side network are fast and stable. Just use the method you wrote yourself