note 56489 added to function.filesize

From: Date: Mon, 05 Sep 2005 00:33:20 +0000
Subject: note 56489 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-94731@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 OR change the regex line to: <?php preg_match("/^([0-9\.]+)[a-zA-Z]?[\s]+[.*]/", $filesize, $matches); ?> ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56489 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56489&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56489&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56489&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56489&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56489&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56489&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56489&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94731) next »