note 56514 modified in function.filesize by betz
| From: | betz@php.net | Date: | Tue, 06 Sep 2005 08:40:47 +0000 |
| Subject: | note 56514 modified in function.filesize by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94790@lists.php.net to get a copy of this message | ||
A different way to get the filesize using a shell 'hack' (and therefore make your code
somewhat platform dependent) is to do the following:
<?php
$file = "/test/file/location/test.txt";
$filesize = exec("du -h ${file}");
preg_match("/^([0-9\.]+[a-zA-Z]?)[\s]+[.*]/", $filesize, $matches);
$filesize = $matches[1];
?>
The regex could probably be cleaner but it does the job. Note that the -h parameter I use here
automatically formats the file size. Hence when I run this on an example file the value in
$matches[1] is:
7.6M
If you just want the raw size in bytes (but without problems when you hit 2 gig etc.) then remove
the -h parameter from the exec line.
--was--
[Self correction of my previous post, if admin could delete the earlier one it would be most
appreciated]
A different way to get the filesize using a shell 'hack' (and therefore make your code
somewhat platform dependent) is to do the following:
<?php
$file = "/test/file/location/test.txt";
$filesize = exec("du -h ${file}");
preg_match("/^([0-9\.]+[a-zA-Z]?)[\s]+[.*]/", $filesize, $matches);
$filesize = $matches[1];
?>
The regex could probably be cleaner but it does the job. Note that the -h parameter I use here
automatically formats the file size. Hence when I run this on an example file the value in
$matches[1] is:
7.6M
If you just want the raw size in bytes (but without problems when you hit 2 gig etc.) then remove
the -h parameter from the exec line.
http://php.net/manual/en/function.filesize.php