note 56489 deleted from function.filesize by betz

From: Date: Tue, 06 Sep 2005 08:39:24 +0000
Subject: note 56489 deleted from function.filesize by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-94789@lists.php.net to get a copy of this message
Note Submitter: flash at houseofzeus dot com ---- 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 OR change the regex line to: <?php preg_match("/^([0-9\.]+)[a-zA-Z]?[\s]+[.*]/", $filesize, $matches); ?>

« previous php.notes (#94789) next »