(PHP 4, PHP 5)
filesize — Liefert die Größe einer Datei
Gibt die Größe einer Datei in Bytes zurück, oder FALSE wenn ein Fehler auftrat.
Das Ergebnis dieses Funktionsaufrufes wird zwischengespeichert. Siehe clearstatcache() für weitere Einzelheiten.
Diese Funktion ist nicht für remote Dateien geeignet, die zu prüfende Datei muss über das Dateisystem des Servers verfügbar sein.
Find filesize of any file ( >2GB works fine )
<?php
function ffilesize($file){
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false)
return false;
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
return (float)$matches[1];
}
?>
And this is my short and very clever simple function. Faster than some of the examples below.
<?php
function format_bytes($size) {
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return round($size, 2).$units[$i];
}
?>
You can add more units to the $units array and change the number 4 in "$i < 4" accordingly.
But if you want it really fast try this not-so-neat function:
<?php
function format_bytes($bytes) {
if ($bytes < 1024) return $bytes.' B';
elseif ($bytes < 1048576) return round($bytes / 1024, 2).' KB';
elseif ($bytes < 1073741824) return round($bytes / 1048576, 2).' MB';
elseif ($bytes < 1099511627776) return round($bytes / 1073741824, 2).' GB';
else return round($bytes / 1099511627776, 2).' TB';
}
?>
I guess you can do it in 1000 different ways :)
It's very interesting but I can't stand the decimals for bytes and KB so here's another example :
<?php
function format_size($size) {
$sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
if ($size == 0) { return('n/a'); } else {
return (round($size/pow(1024, ($i = floor(log($size, 1024)))), $i > 1 ? 2 : 0) . $sizes[$i]); }
}
?>
This is a short and very clever function to get filesizes.
<?php
function format_size($size) {
$sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
if ($size == 0) { return('n/a'); } else {
return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}
?>
Here is a quick function for getting a nicer filesize from a number:
<?php
function formatBytes($b,$p = null) {
/**
*
* @author Martin Sweeny
* @version 2010.0617
*
* returns formatted number of bytes.
* two parameters: the bytes and the precision (optional).
* if no precision is set, function will determine clean
* result automatically.
*
**/
$units = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
$c=0;
if(!$p && $p !== 0) {
foreach($units as $k => $u) {
if(($b / pow(1024,$k)) >= 1) {
$r["bytes"] = $b / pow(1024,$k);
$r["units"] = $u;
$c++;
}
}
return number_format($r["bytes"],2) . " " . $r["units"];
} else {
return number_format($b / pow(1024,$p)) . " " . $units[$p];
}
}
echo formatBytes(81000000); //returns 77.25 MB
echo formatBytes(81000000,0); //returns 81,000,000 B
echo formatBytes(81000000,1); //returns 79,102 kB
?>
Feel free to email me if you can think of improvements!
Get a real filesize value, return as a string, this way it will not have the same effect as filesize() returning an integer that will be limited by 32-bit php systems. Return can be cast to a (float) to work perfectly for files over 2GB
<?php
// WINDOWS SERVERS:
// str ntfs_filesize( str $filename );
/*
DESCRIPTION: returns the filesize of a large file in string format to...
... prevent 32-bit integer walls using windows command line.
*/
function ntfs_filesize($filename)
{
return exec("
for %v in (\"".$filename."\") do @echo %~zv
");
}
// LINUX SERVERS:
// str perl_filesize( str $filename );
/*
DESCRIPTION: returns the filesize of a large file in string format to...
... prevent 32-bit integer walls using perl through linux command line.
*/
function perl_filesize($filename)
{
return exec("
perl -e 'printf \"%d\n\",(stat(shift))[7];' ".$filename."
");
}
// WARNING: For files over 2GB casting integers may give false values, to prevent this cast to a (float):
$file = "C:/my6GBmovie.avi"; // proper filesize: 6442450944 bytes
var_dump(ntfs_filesize($file); // output: string(10) "6442450944"
var_dump((int)ntfs_filesize($file)); // output: int(2147483647)
var_dump((float)ntfs_filesize($file)); // output: float(6442450944)
?>
My solution for calculating the directory size:
<?php
/**
* Get the directory size
* @param directory $directory
* @return integer
*/
function dirSize($directory) {
$size = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
$size+=$file->getSize();
}
return $size;
}
?>
Here's the best way (that I've found) to get the size of a remote file. Note that HEAD requests don't get the actual body of the request, they just retrieve the headers. So making a HEAD request to a resource that is 100MB will take the same amount of time as a HEAD request to a resource that is 1KB.
<?php
$remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
$contentLength = '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)) {
$contentLength = (int)$matches[1];
}
echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>
Result:
HTTP Status: 302
Content-Length: 8808759
This is an updated version of my previous filesize2bytes.
The return type now it's really an int.
<?php
/**
* Converts human readable file size (e.g. 10 MB, 200.20 GB) into bytes.
*
* @param string $str
* @return int the result is in bytes
* @author Svetoslav Marinov
* @author http://slavi.biz
*/
function filesize2bytes($str) {
$bytes = 0;
$bytes_array = array(
'B' => 1,
'KB' => 1024,
'MB' => 1024 * 1024,
'GB' => 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024,
'PB' => 1024 * 1024 * 1024 * 1024 * 1024,
);
$bytes = floatval($str);
if (preg_match('#([KMGTP]?B)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) {
$bytes *= $bytes_array[$matches[1]];
}
$bytes = intval(round($bytes, 2));
return $bytes;
}
?>
a quick way to convert bytes to a more readable format can be done using this function:
<?php
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
?>
Shortest solution to get filesize of a remote website
<?php
$content = file_get_contents("http://www.example.com");
$handle = fopen("example-com.html", "w+");
fwrite($handle, $content);
fclose($handle);
echo size . ': ' . filesize('example-com.html') . ' bytes';
?>
reliably, quick & dirty:
<?php
function getdirsize($path)
{
$result=explode("\t",exec("du -hs ".$path),2);
return ($result[1]==$path ? $result[0] : "error");
}
?>
Fix 4Gb limit. ( Now limit 8 Gb ;))
<?php
function GetRealSize($file) {
// Return size in Mb
clearstatcache();
$INT = 4294967295;//2147483647+2147483647+1;
$size = filesize($file);
$fp = fopen($file, 'r');
fseek($fp, 0, SEEK_END);
if (ftell($fp)==0) $size += $INT;
fclose($file);
if ($size<0) $size += $INT;
return ceil($size/1024/1024);
}
?>
I use the CLI version of PHP on Windows Vista and work with large (~ 10G) Virtual Machine files. Here's how to get a NTFS file's size without the 4G limit:
<?php
function dos_filesize($fn) {
if (is_file($fn))
return exec('FOR %A IN ("'.$fn.'") DO @ECHO %~zA');
else
return '0';
}
?>
This should work on any Windows OS that provides DOS shell commands.
Another way for remote files -> strlen(join('',file('http://www.example.com/)));
<?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;
}
?>
In case of you have a redirection in the server (like Redirect Permanent in the .htaccess)
In this case we have for exemple:
[content-length] => Array
(
[0] => 294 // Size requested file
[1] => 357556 // Real Size redirected file
)
Another workaround for the >2GB "stat failed" in Linux is to call the systems stat as follows:
$size = exec ('stat -c %s '. escapeshellarg ($file_path));
I have created a handy function, using parts of code from kaspernj at gmail dot com and md2perpe at gmail dot com, which should get file sizes > 4GB on Windows, Linux and Mac (at least).
<?php
function getSize($file) {
$size = filesize($file);
if ($size < 0)
if (!(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'))
$size = trim(`stat -c%s $file`);
else{
$fsobj = new COM("Scripting.FileSystemObject");
$f = $fsobj->GetFile($file);
$size = $file->Size;
}
return $size;
}
?>
On 64-bit platforms, this seems quite reliable for getting the filesize of files > 4GB
<?php
$a = fopen($filename, 'r');
fseek($a, 0, SEEK_END);
$filesize = ftell($a);
fclose($a);
?>
filesize() acts differently between platforms and distributions.
I tried manually compiling PHP on a 32bit platform. Filesize() would fail on files >2G.
Then I compiled again, adding CFLAGS=`getconf LFS_CFLAGS` in front of configure.
Then filesize() would success, but the result would be converted to a 32bit signed integer...
However on 64bit systems, PHP's integers are 64bit signed, and this would work just well...
So now the question is : should linux distributions (Debian, etc) define LFS_CFLAGS or not ? Doing so makes PHP be able to open/seek such files, but makes its behaviour buggy (stat() is supposed to fail if file size is >32bit and appl does not support such integers)...
Another way to get a filesize > 2GB on Linux:
<?php
$size = trim(`stat -c%s $file_path`);
?>
Here's a dodgy little snippet I made to find out how much disk space (in 1K-blocks) any chosen node - file or directory - occupies in the file system..
<?php
function nodesize( $node )
{
if( !is_readable($node) ) return false;
$blah = exec( "/usr/bin/du -sk $node" );
return substr( $blah, 0, strpos($blah, 9) );
}
?>
The character I search for with strpos() is a tab, as this is what 'du' on OpenBSD uses to separate size and node in the output - this might be whitespaces in other *nixes/distributions.
I know there has been alot of remote filesize snippets posted, but I'll post mine also.
It supports HTTP/HTTPS/FTP/FTPS and detects which type it should use. It needs --enable-ftp for the FTP/FTPS functions.
I hope this works for someone.
<?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;
}
}
?>
To get the size of files above 2GB you can use the linux-command filesize like this:
<?php
function real_filesize_linux($file) {
@exec("filesize $file",$out,$ret);
if ( $ret <> '0' ) return FALSE;
else return($out[0]);
}
?>
If you want to get the actual filesize for a size above 2 gb in Windows, you can use the COM-extensions in PHP.
An example is as follows:
<?php
function knj_filesize($file){
if (file_exists($file)){
$fsobj = new COM("Scripting.FileSystemObject");
$file = $fsobj->GetFile($file);
$var = ($file->Size) + 1 - 1;
return $var;
}else{
echo "File does not exist.\n";
return false;
}
}
?>
This will return the corrent filesize. And it is very useful with PHP-GTK applications, where you want to use the filesize for larger files.
This example also works for files over a Windows-network. Try this example with the function:
<?php
echo knj_filesize("//mycomputer/music/Track1.mp3");
?>
Happy hacking :)
some notes and modifications to previous post.
refering to RFC, when using HTTP/1.1 your request (either GET or POST or HEAD) must contain Host header string, opposite to HTTP/1.1 where Host ain't required. but there's no sure how your remote server would treat the request so you can add Host anyway (it won't be an error for HTTP/1.0).
host value _must_ be a host name (not CNAME and not IP address).
this function catches response, containing Location header and recursively sends HEAD request to host where we are moved until final response is met.
(you can experience such redirections often when downloading something from php scripts or some hash links that use apache mod_rewrite. most all of dowloading masters handle 302 redirects correctly, so this code does it too (running recursively thru 302 redirections).)
[$counter302] specify how much times your allow this function to jump if redirections are met. If initial limit (5 is default) expired -- it returns 0 (should be modified for your purposes whatever).0
ReadHeader() function is listed in previous post
(param description is placed there too).
<?php
function remote_filesize_thru( $ipAddress, $url, $counter302 = 5 )
{
$socket = fsockopen( "10.233.225.2", 8080 );
if( !$socket )
{
// failed to open TCP socket connection
// do something sensible here besides exit();
echo "<br>failed to open socket for [$ipAddress]";
exit();
}
// just send HEAD request to server
$head = "HEAD $url HTTP/1.0\r\nConnection: Close\r\n\r\n";
// you may use HTTP/1.1 instead, then your request head string _must_ contain "Host: " header
fwrite( $socket, $head );
// read the response header
$header = ReadHeader( $socket );
if( !$header )
{
// handle empty response here the way you need...
Header( "HTTP/1.1 404 Not Found" );
exit();
}
fclose( $socket );
// check for "Location" header
$locationMarker = "Location: ";
$pos = strpos( $header, $locationMarker );
if( $pos > 0 )
{
$counter302--;
if( $counter302 < 0 )
{
// redirect limit (5 by default) expired -- return some warning or do something sensible here
echo "warning: too long redirection sequence";
return 0;
}
// Location is present -- we should determine target host and move there, like any downloading masters do...
// no need to use regex here
$end = strpos( $header, "\n", $pos );
$location = trim( substr( $header, $pos + strlen( $locationMarker ), $end - $pos - strlen( $locationMarker ) ), "\\r\\n" );
// extract pure host (without "http://")
$host = explode( "/", $location );
$ipa = gethostbyname( $host[2] );
// move to Location
return remote_filesize_thru( $ipa, $location, $counter302 );
}
// try to acquire Content-Length within the response
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $header, $matches);
// if there was a Content-Length field, its value
// will now be in $matches[1]
if( isset( $matches[1] ) )
$size = $matches[1];
else
$size = 0;
return $size;
}
?>
this is "raw" version of remote_filesize() function.
according to RFC, HTTP servers MUST implement at least GET, POST and HEAD requests, so the function just opens TCP socket connection, sends HEAD request and receives response, parsing length of the resource.
[$ipAddress] is the ip address of remote server.
[$url] is the name of file which size you want to determine.
the code was tested under Apache 2.0.43 and IIS 6.0 and it works correctly in both cases.
i wish the code can save someone's time :)
example:
$ipa = gethostbyname( "www.someserver.com" );
$url = "/docs/somedocument.pdf";
$fsize = remote_filesize2( $ipa, $url );
==========================
<?php
function ReadHeader( $socket )
{
$i=0;
$header = "";
while( true && $i<20 )
{
// counter [$i] is used here to avoid deadlock while reading header string
// it's limited by [20] here cause i really haven't ever met headers with string counter greater than 20
// *
$s = fgets( $socket, 4096 );
$header .= $s;
if( strcmp( $s, "\r\n" ) == 0 || strcmp( $s, "\n" ) == 0 )
break;
$i++;
}
if( $i >= 20 )
{
// suspicious header strings count was read
// *
return false;
}
return $header;
}
function remote_filesize2( $ipAddress, $url )
{
$socket = fsockopen( $ipAddress, 80 );
if( !$socket )
{
// failed to open TCP socket connection
// do something sensible here besides exit();
// ...
exit();
}
// just send HEAD request to server
// *
fwrite( $socket, "HEAD $url HTTP/1.0\r\nConnection: Close\r\n\r\n" );
// read the response header
// *
$header = ReadHeader( $socket );
if( !$header )
{
Header( "HTTP/1.1 404 Not Found" );
exit();
}
// try to acquire Content-Length within the response
// *
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $header, $matches);
// if there was a Content-Length field, its value
// will now be in $matches[1]
if( isset( $matches[1] ) )
{
$size = $matches[1];
}
else
{
$size = 0;
}
fclose( $socket );
return $size;
}
?>
When read/writing binary files you often cannot rely on the feof() function being of much use, since it doesn't get triggered if the pointer is at the eof but hasn't tried to read one more byte. In this case you instead need to check if the file pointer is at filesize yet, but if you don't have the filename handy, you need to pluck it out fstat all the time. Two simple functions that would be nice to have natively in PHP:
<?php
function fpfilesize(&$fp) { $stat = fstat($fp); return $stat["size"]; }
function fpeof(&$fp) { return ftell($fp)==fpfilesize($fp); }
?>
This function quickly calculates the size of a directory:
http://aidanlister.com/repos/v/function.dirsize.php
You can convert filesizes to a human readable size using:
http://aidanlister.com/repos/v/function.size_readable.php
For a faster (unix only) implementation, see function.disk-total-space, note #34100
http://www.php.net/manual/en/function.disk-total-space.php#34100
Also of interest is this wikipedia article, discussing the difference between a kilobyte (1000) and a kibibyte (1024).
http://en.wikipedia.org/wiki/Bytes
In addition to the handy function Kris posted, here is an upgraded version that does basic http authentication as well.
<?php
/*
* (mixed)remote_filesize($uri,$user='',$pw='')
* returns the size of a remote stream in bytes or
* the string 'unknown'. Also takes user and pw
* incase the site requires authentication to access
* the uri
*/
function remote_filesize($uri,$user='',$pw='')
{
// start output buffering
ob_start();
// initialize curl with given uri
$ch = curl_init($uri);
// make sure we get the header
curl_setopt($ch, CURLOPT_HEADER, 1);
// make it a http HEAD request
curl_setopt($ch, CURLOPT_NOBODY, 1);
// if auth is needed, do it here
if (!empty($user) && !empty($pw))
{
$headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$okay = curl_exec($ch);
curl_close($ch);
// get the output buffer
$head = ob_get_contents();
// clean the output buffer and return to previous
// buffer settings
ob_end_clean();
// gets you the numeric value from the Content-Length
// field in the http header
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
// if there was a Content-Length field, its value
// will now be in $matches[1]
if (isset($matches[1]))
{
$size = $matches[1];
}
else
{
$size = 'unknown';
}
return $size;
}
?>