Home > Backend Development > PHP Tutorial > curl - PHP How to quickly determine whether a remote file exists

curl - PHP How to quickly determine whether a remote file exists

WBOY
Release: 2016-08-04 09:19:45
Original
1161 people have browsed it

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>
Copy after login
Copy after login

This unified path is not very stable. Sometimes it succeeds and sometimes it fails, and the waiting time is too long!

Reply content:

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template