Удаленный размер файла без загрузки файла - PullRequest
73 голосов
/ 08 апреля 2010

Есть ли способ получить размер удаленного файла http://my_url/my_file.txt без загрузки файла?

Ответы [ 13 ]

94 голосов
/ 08 апреля 2010

Нашли что-то об этом здесь :

Вот лучший способ (который я нашел), чтобы получить размер удаленного файл. Обратите внимание, что запросы HEAD не получают фактическое тело запроса, они просто получают заголовки. Таким образом, сделать запрос HEAD к ресурсу это 100 МБ займет столько же времени, сколько запрос HEAD к ресурс размером 1 КБ.

<?php
/**
 * Returns the size of a file without downloading it, or -1 if the file
 * size could not be determined.
 *
 * @param $url - The location of the remote file to download. Cannot
 * be null or empty.
 *
 * @return The size of the file referenced by $url, or -1 if the size
 * could not be determined.
 */
function curl_get_file_size( $url ) {
  // Assume failure.
  $result = -1;

  $curl = curl_init( $url );

  // Issue a HEAD request and follow any redirects.
  curl_setopt( $curl, CURLOPT_NOBODY, true );
  curl_setopt( $curl, CURLOPT_HEADER, true );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $curl, CURLOPT_USERAGENT, get_user_agent_string() );

  $data = curl_exec( $curl );
  curl_close( $curl );

  if( $data ) {
    $content_length = "unknown";
    $status = "unknown";

    if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
      $status = (int)$matches[1];
    }

    if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
      $content_length = (int)$matches[1];
    }

    // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    if( $status == 200 || ($status > 300 && $status <= 308) ) {
      $result = $content_length;
    }
  }

  return $result;
}
?>

Использование:

$file_size = curl_get_file_size( "/1430135/udalennyi-razmer-faila-bez-zagruzki-faila" );
60 голосов
/ 17 ноября 2011

Попробуйте этот код

function retrieve_remote_file_size($url){
     $ch = curl_init($url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_HEADER, TRUE);
     curl_setopt($ch, CURLOPT_NOBODY, TRUE);

     $data = curl_exec($ch);
     $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

     curl_close($ch);
     return $size;
}
28 голосов
/ 18 сентября 2014

Как уже упоминалось пару раз, можно получить информацию из поля Content-Length заголовка ответа .

Однако вы должны отметить, что

  • сервер, который вы исследуете, не обязательно реализует метод HEAD (!)
  • абсолютно не нужно вручную создавать запрос HEAD (который, опять-таки, может даже не поддерживаться), используя fopen или тому подобное или даже вызывать библиотеку curl, когда PHP имеет get_headers() (помните: ПОЦЕЛУЙ )

Использование get_headers() следует за K.I.S.S. Принцип и работает, даже если исследуемый сервер не поддерживает запрос HEAD.

Итак, вот моя версия (трюк: возвращает читаемый отформатированный размер; -)):

Суть: https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d (версия curl и get_headers)
get_headers () - версия:

<?php     
/**
 *  Get the file size of any remote resource (using get_headers()), 
 *  either in bytes or - default - as human-readable formatted string.
 *
 *  @author  Stephan Schmitz <eyecatchup@gmail.com>
 *  @license MIT <http://eyecatchup.mit-license.org/>
 *  @url     <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d>
 *
 *  @param   string   $url          Takes the remote object's URL.
 *  @param   boolean  $formatSize   Whether to return size in bytes or formatted.
 *  @param   boolean  $useHead      Whether to use HEAD requests. If false, uses GET.
 *  @return  string                 Returns human-readable formatted size
 *                                  or size in bytes (default: formatted).
 */
function getRemoteFilesize($url, $formatSize = true, $useHead = true)
{
    if (false !== $useHead) {
        stream_context_set_default(array('http' => array('method' => 'HEAD')));
    }
    $head = array_change_key_case(get_headers($url, 1));
    // content-length of download (in bytes), read from Content-Length: field
    $clen = isset($head['content-length']) ? $head['content-length'] : 0;

    // cannot retrieve file size, return "-1"
    if (!$clen) {
        return -1;
    }

    if (!$formatSize) {
        return $clen; // return size in bytes
    }

    $size = $clen;
    switch ($clen) {
        case $clen < 1024:
            $size = $clen .' B'; break;
        case $clen < 1048576:
            $size = round($clen / 1024, 2) .' KiB'; break;
        case $clen < 1073741824:
            $size = round($clen / 1048576, 2) . ' MiB'; break;
        case $clen < 1099511627776:
            $size = round($clen / 1073741824, 2) . ' GiB'; break;
    }

    return $size; // return formatted size
}

Использование:

$url = 'http://download.tuxfamily.org/notepadplus/6.6.9/npp.6.6.9.Installer.exe';
echo getRemoteFilesize($url); // echoes "7.51 MiB"

Дополнительное примечание: Заголовок Content-Length является необязательным. Таким образом, общее решение не является пуленепробиваемым !


14 голосов
/ 08 апреля 2010

Конечно. Сделайте запрос только для заголовков и найдите заголовок Content-Length.

7 голосов
/ 20 апреля 2017

Php функция get_headers() работает для меня, чтобы проверить content-length как

$headers = get_headers('http://example.com/image.jpg', TRUE);
$filesize = $headers['content-length'];

Подробнее: PHP функция get_headers ()

6 голосов
/ 25 мая 2014

Я не уверен, но не могли бы вы использовать для этого функцию get_headers?

$url     = 'http://example.com/dir/file.txt';
$headers = get_headers($url, true);

if ( isset($headers['Content-Length']) ) {
   $size = 'file size:' . $headers['Content-Length'];
}
else {
   $size = 'file size: unknown';
}

echo $size;
3 голосов
/ 05 мая 2014

Самая простая и эффективная реализация:

function remote_filesize($url, $fallback_to_download = false)
{
    static $regex = '/^Content-Length: *+\K\d++$/im';
    if (!$fp = @fopen($url, 'rb')) {
        return false;
    }
    if (isset($http_response_header) && preg_match($regex, implode("\n", $http_response_header), $matches)) {
        return (int)$matches[0];
    }
    if (!$fallback_to_download) {
        return false;
    }
    return strlen(stream_get_contents($fp));
}
2 голосов
/ 02 января 2013

Попробуйте следующую функцию, чтобы получить размер удаленного файла

function remote_file_size($url){
    $head = "";
    $url_p = parse_url($url);

    $host = $url_p["host"];
    if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){

        $ip=gethostbyname($host);
        if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){

            return -1;
        }
    }
    if(isset($url_p["port"]))
    $port = intval($url_p["port"]);
    else
    $port    =    80;

    if(!$port) $port=80;
    $path = $url_p["path"];

    $fp = fsockopen($host, $port, $errno, $errstr, 20);
    if(!$fp) {
        return false;
        } else {
        fputs($fp, "HEAD "  . $url  . " HTTP/1.1\r\n");
        fputs($fp, "HOST: " . $host . "\r\n");
        fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        $headers = "";
        while (!feof($fp)) {
            $headers .= fgets ($fp, 128);
            }
        }
    fclose ($fp);

    $return = -2;
    $arr_headers = explode("\n", $headers);
    foreach($arr_headers as $header) {

        $s1 = "HTTP/1.1";
        $s2 = "Content-Length: ";
        $s3 = "Location: ";

        if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1));
        if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size   = substr($header, strlen($s2));
        if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3));  
    }

    if(intval($size) > 0) {
        $return=intval($size);
    } else {
        $return=$status;
    }

    if (intval($status)==302 && strlen($newurl) > 0) {

        $return = remote_file_size($newurl);
    }
    return $return;
}
2 голосов
/ 08 апреля 2010

Поскольку этот вопрос уже помечен как "php" и "curl", я предполагаю, что вы знаете, как использовать Curl в PHP.

Если вы установите curl_setopt(CURLOPT_NOBODY, TRUE), то вы сделаете запрос HEAD и, возможно, сможете проверить заголовок ответа «Content-Length», который будет только заголовком.

1 голос
/ 19 декабря 2017

Лучшее решение в одну строку:

echo array_change_key_case(get_headers("http://.../file.txt",1))['content-length'];

PHP слишком делициус

function urlsize($url):int{
   return array_change_key_case(get_headers($url,1))['content-length'];
}

echo urlsize("http://.../file.txt");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...