note 28686 deleted from function.filesize by tomsommer

From: Date: Wed, 01 Sep 2004 17:33:08 +0000
Subject: note 28686 deleted from function.filesize by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75880@lists.php.net to get a copy of this message
Note Submitter: brodie-php at tmts dot ca ---- The following code builds on the some of the above comments. It reads a directory for a particular file type (.mp3) and then lists the files and their sizes. In this case, I wanted to put some special mp3s in a directory and be able to add to the list easily without changing the webpage. <?php //$imagedir is the absolute path to the directory wherein you wish to store special mp3s //example: $imagedir = "/usr/local/www/data/user/dir"; $imagedir = "C:/Windows/User/Dir"; //current directory $i = 1; if ($handle = opendir("$imagedir")) { while (false !== ($file = readdir($handle))) { //strncmp returns < 0 if arg1 is less, >0 if more, 0 if equal $extension = substr($file, -4, 4); if ($file != "." && $file != ".." && ($extension == ".mp3")) { echo($i++.".&nbsp;&nbsp; <a href=\"$file\">$file (".fsize($file).")</a><br>"); } } closedir($handle); }else{ echo("<br>Directory could not be opened.<br>"); } function fsize($file) { // Does the file exist? if(is_file($imagedir.$file)){ //Setup some common file size measurements. $kb=1024; $mb=1048576; $gb=1073741824; $tb=1099511627776; //Get the file size in bytes. $size = filesize($file); //Format file size if($size < $kb) { return $size." B"; } else if($size < $mb) { return round($size/$kb,2)." KB"; } else if($size < $gb) { return round($size/$mb,2)." MB"; } else if($size < $tb) { return round($size/$gb,2)." GB"; } else { return round($size/$tb,2)." TB"; } } } ?>

« previous php.notes (#75880) next »