Get folder size. Please help.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Get folder size. Please help.

Post 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;
}
Last edited by crzyman on Sun Sep 18, 2005 6:15 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Post by crzyman »

Thanks. I will look that up and see if I can't make this work.
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

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

Post 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;
	}
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

hardcode $max_file_size into your script and see what that does
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Post by crzyman »

That worked! Why? I would like to know.
Post Reply