note 45787 deleted from function.filesize by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 19 Sep 2004 18:27:32 +0000 |
| Subject: | note 45787 deleted from function.filesize by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76986@lists.php.net to get a copy of this message | ||
Note Submitter: info at b1g dot de
----
Just a dirty, small function to get the file size of a remote HTTP file. The functions tries to get
the Content-Length-Header first. If this fails, it downloads the whole file and returns the file
size using strlen(). Works fine for me, you might find it useful.
<?php
function httpFileSize($file)
{
ereg("http:\/\/([^\/]*)\/(.*)", $file, $reg);
$uri = '/' . $reg[2];
if(strstr($reg[1], ':') !== false)
{
list($host, $port) = explode(':', $reg[1]);
}
else
{
$host = $reg[1];
$port = 80;
}
$get = 'GET ' . $uri . ' HTTP/1.1' . "\r\n" . 'Host: ' .
$host . "\r\n\r\n";
$sock = fsockopen($host, $port);
fwrite($sock, $get, strlen($get));
$res = fread($sock, 1024);
fclose($sock);
if(eregi("Content-Length: ([0-9]*)", $res, $reg))
{
return $reg[1];
}
else
{
$i = 0;
$fp = fopen($file, 'r');
while(!feof($fp))
{
$i += strlen(fread($fp, 4096));
}
fclose($fp);
return $i;
}
}
?>