Get folder size. Please help.
Posted: Sat Sep 17, 2005 11:29 am
I am trying to get the total size of a folder and if the new upload to that folder exceeds the max limit of the folder a error message gets returned. Here is a part of the code that I have. I can't seem to get it to work.
Code: Select all
<?
$bandname = "Test";
$storage_dir = "data/$bandname"; // storage directory (chmod 777)
$max_filesize = 15 * pow(1024,2); // maximum filesize (x MiB)
$allowed_fileext = array("mp3");// allowed extensions
if (!is_dir("data/$bandname")) {
if (!mkdir($storage_dir))
die ("upload_files directory doesn't exist and creation failed");
if (!chmod($storage_dir,0777))
die ("change permission to 777 failed.");
}
if (isset($_FILES['file']))
uploadfile($_FILES['file']);
function uploadfile($file) {
global $storage_dir, $max_filesize, $allowed_fileext, $errormsg;
if ($file['error']!=0) {
switch ($file['error']) {
case 1: $errormsg = "The uploaded file exceeds the upload_max_filesize directive in php.ini"; break;
case 2: $errormsg = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."; break;
case 3: $errormsg = "The uploaded file was only partially uploaded."; break;
case 4: $errormsg = "No file was uploaded."; break;
case 6: $errormsg = "Missing a temporary folder."; break;
}
return;
}
$filesource=$file['tmp_name'];
$filename=$file['name'];
if (isset($_POST['filename']) && $_POST['filename']!="") $filename=$_POST['filename'];
if (!in_array(strtolower(extname($filename)), $allowed_fileext)) $filename .= ".badext";
$filesize=$file['size'];
//This is where I want to get the total size of the folder
$total += filesize($storage_dir);
//and if it is greater than the allowed size returns this error.
if ($total > $max_filesize) {
$errormsg = "Total file size is greater than the total limit (".getfilesize($total).").";
return;
}