31-10-2004, 21:58
|
|
|
|
חבר מתאריך: 09.12.01
הודעות: 2,337
|
|
הנה פונקציה נחמדה אשר תהפוך את הגודל של הביטים למשהו קצת יותר ברור.
קוד PHP:
function my_filesize($file) { // First check if the file exists. if(!is_file("./".$file)) exit("File does not exist!"); // Setup some common file size measurements. $kb = 1024; // Kilobyte $mb = 1048576; // Megabyte $gb = 1073741824; // Gigabyte $tb = 1099511627776;// Terabyte // Get the file size in bytes. $size = filesize($file); /* If it's less than a kb we just return the size, otherwise we keep going until the size is in the appropriate measurement range. */ 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"; } }
ופונקציה קצת יותר מסודרת וקצרה ונוחה יותר:
קוד PHP:
function fsize($file) { $a = array("B", "KB", "MB", "GB", "TB", "PB"); $pos = 0; $size = filesize($file); while ($size >= 1024) { $size /= 1024; $pos++; } return round($size,2)." ".$a[$pos]; }
_____________________________________
“The man of wisdom is never in two minds about right and wrong;
the man of benevolence never worries about the future;
the man of courage is never afraid.”
~ Confucius
|