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

前端 未结 22 2547
死守一世寂寞
死守一世寂寞 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:35

    You can use the filesystem: use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

    and check $fileSystem = new Filesystem(); if ($fileSystem->exists('path_to_file')==true) {...

    0 讨论(0)
  • 2020-11-22 06:37
    if (false === file_get_contents("http://example.com/path/to/image")) {
        $image = $default_image;
    }
    

    Should work ;)

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

    all the answers here that use get_headers() are doing a GET request. It's much faster/cheaper to just do a HEAD request.

    To make sure that get_headers() does a HEAD request instead of a GET you should add this:

    stream_context_set_default(
        array(
            'http' => array(
                'method' => 'HEAD'
            )
        )
    );
    

    so to check if a file exists, your code would look something like this:

    stream_context_set_default(
        array(
            'http' => array(
                'method' => 'HEAD'
            )
        )
    );
    $headers = get_headers('http://website.com/dir/file.jpg', 1);
    $file_found = stristr($headers[0], '200');
    

    $file_found will return either false or true, obviously.

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

    CoolGoose's solution is good but this is faster for large files (as it only tries to read 1 byte):

    if (false === file_get_contents("http://example.com/path/to/image",0,null,0,1)) {
        $image = $default_image;
    }
    
    0 讨论(0)
  • 2020-11-22 06:38

    This can be done by obtaining the HTTP Status code (404 = not found) which is possible with file_get_contentsDocs making use of context options. The following code takes redirects into account and will return the status code of the final destination (Demo):

    $url = 'http://example.com/';
    $code = FALSE;
    
    $options['http'] = array(
        'method' => "HEAD",
        'ignore_errors' => 1
    );
    
    $body = file_get_contents($url, NULL, stream_context_create($options));
    
    foreach($http_response_header as $header)
        sscanf($header, 'HTTP/%*d.%*d %d', $code);
    
    echo "Status code: $code";
    

    If you don't want to follow redirects, you can do it similar (Demo):

    $url = 'http://example.com/';
    $code = FALSE;
    
    $options['http'] = array(
        'method' => "HEAD",
        'ignore_errors' => 1,
        'max_redirects' => 0
    );
    
    $body = file_get_contents($url, NULL, stream_context_create($options));
    
    sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);
    
    echo "Status code: $code";
    

    Some of the functions, options and variables in use are explained with more detail on a blog post I've written: HEAD first with PHP Streams.

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

    To check for the existence of images, exif_imagetype should be preferred over getimagesize, as it is much faster.

    To suppress the E_NOTICE, just prepend the error control operator (@).

    if (@exif_imagetype($filename)) {
      // Image exist
    }
    

    As a bonus, with the returned value (IMAGETYPE_XXX) from exif_imagetype we could also get the mime-type or file-extension with image_type_to_mime_type / image_type_to_extension.

    0 讨论(0)
提交回复
热议问题