0

How to check file size in via Internet? The sample below is my code that does not work

echo filesize('http://localhost/wordpress-3.1.2.zip');
echo filesize('http://www.wordpress.com/wordpress-3.1.2.zip');
Trinimon
  • 13,839
  • 9
  • 44
  • 60
DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • 2
    Possible dublicate: http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file – Peter Apr 14 '13 at 15:47
  • 1
    You can check the `Content-Length` header, although it isn't guaranteed to be accurate. Most stable way is to download the file to the server in a temp location, then run `filesize()` on it. – nickb Apr 14 '13 at 15:48

3 Answers3

3

The filesize function is used to get size of files stored locally.* For remote files you must find other solution, for example:

<?php
function getSizeFile($url) {
    if (substr($url,0,4)=='http') {
        $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
        if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; }
        else { $x = $x['content-length']; }
    }
    else { $x = @filesize($url); }

    return $x;
}
?> 

Source: See the first post-comment in link below

http://php.net/manual/en/function.filesize.php

*Well, to be honest since PHP 5 there are some wrappers for file functions, see here:

http://www.php.net/manual/en/wrappers.php

You can find a lot more examples, even here on SO, this should satisfy your needs: PHP: Remote file size without downloading file

Try to use search function before asking question next time!

Community
  • 1
  • 1
Wookie88
  • 33,079
  • 4
  • 27
  • 32
0

try this function

<?php
    function remotefsize($url) {
        $sch = parse_url($url, PHP_URL_SCHEME);
        if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) {
            return false;
        }
        if (($sch == "http") || ($sch == "https")) {
            $headers = get_headers($url, 1);
            if ((!array_key_exists("Content-Length", $headers))) { return false; }
            return $headers["Content-Length"];
        }
        if (($sch == "ftp") || ($sch == "ftps")) {
            $server = parse_url($url, PHP_URL_HOST);
            $port = parse_url($url, PHP_URL_PORT);
            $path = parse_url($url, PHP_URL_PATH);
            $user = parse_url($url, PHP_URL_USER);
            $pass = parse_url($url, PHP_URL_PASS);
            if ((!$server) || (!$path)) { return false; }
            if (!$port) { $port = 21; }
            if (!$user) { $user = "anonymous"; }
            if (!$pass) { $pass = "phpos@"; }
            switch ($sch) {
                case "ftp":
                    $ftpid = ftp_connect($server, $port);
                    break;
                case "ftps":
                    $ftpid = ftp_ssl_connect($server, $port);
                    break;
            }
            if (!$ftpid) { return false; }
            $login = ftp_login($ftpid, $user, $pass);
            if (!$login) { return false; }
            $ftpsize = ftp_size($ftpid, $path);
            ftp_close($ftpid);
            if ($ftpsize == -1) { return false; }
            return $ftpsize;
        }
    }
?>
Mihai Vilcu
  • 1,947
  • 18
  • 24
-1

I think that's probably not possible. The best way is to download the file via file_get_contents and then use filesize over the file. You can later delete the file too!

Rishabh
  • 1,901
  • 2
  • 19
  • 18