PHP: How to get size of a remote file


With a local file, we can get file size of it by using filesize, but with a remote file, how to get size of a remote file?

We offer some methods to get size of a remote file

Method 1: use CURL

<?php

/**
 * @author tutorialspots.com
 * @copyright 2015
 */

function get_size($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    $r = curl_exec($curl);
    foreach (explode("\n", $r) as $header)
    {
        if (strpos($header, 'Content-Length:') === 0)
        {
            return trim(substr($header, 16));
        }
    }
    return $r;
}
?>

Example 1:

var_dump(get_size("http://mirror.internode.on.net/pub/test/5meg.test1"));

Result:

string(7) "5242880"

Example 2:

var_dump(get_size("http://tutorialspots.com/wp-content/uploads/2013/04/1.png"));

Result:

string(4) "3955"

Method 2:

<?php

/**
 * @author tutorialspots.com
 * @copyright 2015
 */

function get_size($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
    curl_setopt($curl, CURLOPT_HEADER, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    $r = curl_exec($curl);

    foreach (explode("\n", $r) as $header)
    {
        if (strpos($header, 'Content-Length:') === 0)
        {
            return trim(substr($header, 16));
        }
    }
    return $r;
}
?>

Method 3: use fsockopen

<?php

/**
 * @author tutorialspots.com
 * @copyright 2015
 */

function get_size($url)
{
    $urlInfo = parse_url($url);
    $out = "GET  {$url} HTTP/1.1\r\n";
    $out .= "Host: {$urlInfo['host']}\r\n";
    $out .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
    $out .= "Connection: Close\r\n\r\n";
    $con = @fsockopen($urlInfo['host'], 80, $errno, $errstr, 20);
    if (!$con)
    {
        return $errstr . " " . $errno;
    }
    fwrite($con, $out);
    $data = '';
    while (!feof($con))
    {
        $data .= fgets($con, 512);
        if (substr($data, -4) == "\r\n\r\n")
            break;
    }
    fclose($con);
    preg_match("!\r\nContent-Length: *(.*?) *\r\n!", $data, $matches);
    $url = $matches[1];
    return trim($url);
}
?>

Method 4: Similar as method 3

<?php

/**
 * @author tutorialspots.com
 * @copyright 2015
 */

function get_size($url)
{
    $urlInfo = parse_url($url);
    $out = "HEAD  {$url} HTTP/1.1\r\n";
    $out .= "Host: {$urlInfo['host']}\r\n";
    $out .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
    $out .= "Connection: Close\r\n\r\n";
    $con = @fsockopen($urlInfo['host'], 80, $errno, $errstr, 20);
    if (!$con)
    {
        return $errstr . " " . $errno;
    }
    fwrite($con, $out);
    $data = '';
    while (!feof($con))
    {
        $data .= fgets($con, 512);
    }
    fclose($con);
    preg_match("!\r\nContent-Length: *(.*?) *\r\n!", $data, $matches);
    $url = $matches[1];
    return trim($url);
}
?>

Method 5: use stream_get_meta_data

<?php

/**
 * @author tutorialspots.com
 * @copyright 2015
 */

function get_size($url)
{
    $opts = array('http' => array(
        'method' => "GET", 
        'header' => "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"
    ));
    $context = stream_context_create($opts);
    $con = fopen($url, 'r', false, $context);
    $data = stream_get_meta_data($con);
    fclose($con);
    foreach ($data["wrapper_data"] as $header)
    {
        if (strpos($header, 'Content-Length:') === 0)
        {
            return trim(substr($header, 16));
        }
    }
    return false;
}
?>

Note: after using stream_get_meta_data, we get the data:

array(10) {
  ["wrapper_data"]=>
  array(10) {
    [0]=>
    string(15) "HTTP/1.1 200 OK"
    [1]=>
    string(35) "Date: Thu, 10 Dec 2015 13:15:54 GMT"
    [2]=>
    string(30) "Server: Apache/2.2.27 (CentOS)"
    [3]=>
    string(44) "Last-Modified: Mon, 08 Apr 2013 16:59:00 GMT"
    [4]=>
    string(33) "ETag: "7220077-f73-4d9dc5a7b9be0""
    [5]=>
    string(20) "Accept-Ranges: bytes"
    [6]=>
    string(20) "Content-Length: 3955"
    [7]=>
    string(17) "Connection: close"
    [8]=>
    string(23) "Content-Type: image/png"
    [9]=>
    string(24) "X-Pad: avoid browser bug"
  }
  ["wrapper_type"]=>
  string(4) "http"
  ["stream_type"]=>
  string(14) "tcp_socket/ssl"
  ["mode"]=>
  string(2) "r+"
  ["unread_bytes"]=>
  int(1169)
  ["seekable"]=>
  bool(false)
  ["uri"]=>
  string(57) "http://tutorialspots.com/wp-content/uploads/2013/04/1.png"
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
}

Method 6: similar as method 5

<?php

/**
 * @author tutorialspots.com
 * @copyright 2015
 */

function get_size($url)
{
    $opts = array('http' => array(
        'method' => "HEAD", 
        'header' => "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"
    ));
    $context = stream_context_create($opts);
    $con = fopen($url, 'r', false, $context);
    $data = stream_get_meta_data($con);
    fclose($con);
    foreach ($data["wrapper_data"] as $header)
    {
        if (strpos($header, 'Content-Length:') === 0)
        {
            return trim(substr($header, 16));
        }
    }
    return false;
}
?>

Next: PHP: How to get size of a remote file – Part 2

1 Comment

Leave a Reply