Page 1 of 1

Get folder size. Please help.

Posted: Sat Sep 17, 2005 11:29 am
by crzyman
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;
}

Posted: Sat Sep 17, 2005 12:50 pm
by feyd
filesize only works on a file.. you'll have to use an opendir() or glob() loop to find all files in the folder and sum the sizes.

Posted: Sat Sep 17, 2005 1:04 pm
by crzyman
Thanks. I will look that up and see if I can't make this work.

Added function, but code still won't work. Please help.

Posted: Sun Sep 18, 2005 6:14 pm
by crzyman
I added a funtion to get the size of the folder and compare what the size will be if a new file is added agiants the max size allowed. I don't know what I am doing wromg. Can someone please help. Many thanks.

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.");
}
//GET THE SIZE OF THE FOLDER
 function dir_size($dir) { 
   $totalsize=0; 
   if($dirstream = @opendir($dir)) { 
      while(false !== ($filename = readdir($dirstream))) { 
         if($filename!="." && $filename!="..") { 
            if(is_file($dir."/".$filename)) { 
               $totalsize+=filesize($dir."/".$filename); 
            } 
            if(is_dir($dir."/".$filename)) { 
               $totalsize+=dir_size($dir."/".$filename); 
            } 
         } 
      } 
   } 
   closedir($dirstream); 
   return number_format(($totalsize/1024),2); 
}


$dirsize = dir_size("/UPLOADTEST/data/TestMyAss");

    

//START UPLOAD
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";
//GET FILE SIZE AND WHAT THE TOTAL SIZE OF THE FOLDER WILL BE
$filesize=$file['size'];
$test = $filesize+$dirsize;

	if ($filesize > $max_filesize) {
		$errormsg = "File size is greater than the file size limit (".getfilesize($max_filesize).").";
		return;
	}

	$filedest="$storage_dir/$filename";
	if (file_exists($filedest)) {
		$errormsg = "$filename exists already in the storage directory.";
		return;
	}

	if (!copy($filesource,$filedest)) {
		$errormsg = "Unable to copy the file into the storage directory.";
	}
//TEST TO SEE IF TOTAL SIZE IS MORE THAN ALLOWED
        if ($test > $max_filesize) {
		$errormsg = "Total file size is greater than the total limit (".getfilesize($max_filesize).").";
		return;
	}
}

Posted: Sun Sep 18, 2005 6:31 pm
by s.dot
What problem are you having? Your loop looks good.. I didn't check the upload code, so perhaps the error lies there?

Or maybe you don't know what information you're expecting. Your function will return the sum of all files in a folder. To check against a given size you'd have to use a simple if/else.

Some logic:

Loop through all the files of a given directory. Add these numbers together, arriving at $totalsize.
Then, during upload, check the $filesize of the file being uploaded.
Check to see IF $totalsize+$filesize > $specifiedsize
If it is, declare what you want to do, ELSE do something else

Posted: Sun Sep 18, 2005 8:19 pm
by crzyman
Thanks for your time. That the thought process I'm using. I get the total size of the file ($dirsize) and the size of the file to be uploaded ($filesize). I then I get what the total size would be with the new file that is to be uploaded ( $test = $filesize+$dirsize). And then I use the IF STATEMENT:

if ($test > $max_filesize) {
$errormsg = "Total file size is greater than the total limit (".getfilesize($max_filesize).").";
return;
}

To me it look like it should work, but it won't. It has to be something small, but what? This has my brain going nuts. Any help would be greatly appreciated.

Posted: Sun Sep 18, 2005 11:41 pm
by s.dot
hardcode $max_file_size into your script and see what that does

Posted: Mon Sep 19, 2005 6:04 am
by crzyman
That worked! Why? I would like to know.