How can one check to see if a remote file exists using PHP?

前端 未结 22 2587
死守一世寂寞
死守一世寂寞 2020-11-22 05:52

The best I could find, an if fclose fopen type thing, makes the page load really slowly.

Basically what I\'m trying to do is

相关标签:
22条回答
  • 2020-11-22 06:27

    A complete function of the most voted answer:

    function remote_file_exists($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); # handles 301/2 redirects
        curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if( $httpCode == 200 ){return true;}
    }
    

    You can use it like this:

    if(remote_file_exists($url))
    {
        //file exists, do something
    }
    
    0 讨论(0)
  • 2020-11-22 06:27

    You can use :

    $url=getimagesize(“http://www.flickr.com/photos/27505599@N07/2564389539/”);
    
    if(!is_array($url))
    {
       $default_image =”…/directoryFolder/junal.jpg”;
    }
    
    0 讨论(0)
  • 2020-11-22 06:28

    There's an even more sophisticated alternative. You can do the checking all client-side using a JQuery trick.

    $('a[href^="http://"]').filter(function(){
         return this.hostname && this.hostname !== location.hostname;
    }).each(function() {
        var link = jQuery(this);
        var faviconURL =
          link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1')+'/favicon.ico';
        var faviconIMG = jQuery('<img src="favicon.png" alt="" />')['appendTo'](link);
        var extImg = new Image();
        extImg.src = faviconURL;
        if (extImg.complete)
          faviconIMG.attr('src', faviconURL);
        else
          extImg.onload = function() { faviconIMG.attr('src', faviconURL); };
    });
    

    From http://snipplr.com/view/18782/add-a-favicon-near-external-links-with-jquery/ (the original blog is presently down)

    0 讨论(0)
  • 2020-11-22 06:28

    If you're using the Symfony framework, there is also a much simpler way using the HttpClientInterface:

    private function remoteFileExists(string $url, HttpClientInterface $client): bool {
        $response = $client->request(
            'GET',
            $url //e.g. http://example.com/file.txt
        );
    
        return $response->getStatusCode() == 200;
    }
    

    The docs for the HttpClient are also very good and maybe worth looking into if you need a more specific approach: https://symfony.com/doc/current/http_client.html

    0 讨论(0)
  • 2020-11-22 06:33

    You could use the following:

    $file = 'http://mysite.co.za/images/favicon.ico';
    $file_exists = (@fopen($file, "r")) ? true : false;
    

    Worked for me when trying to check if an image exists on the URL

    0 讨论(0)
  • 2020-11-22 06:35

    Don't know if this one is any faster when the file does not exist remotely, is_file(), but you could give it a shot.

    $favIcon = 'default FavIcon';
    if(is_file($remotePath)) {
       $favIcon = file_get_contents($remotePath);
    }
    
    0 讨论(0)
提交回复
热议问题