Jump to content

how to get directory/folder size and delete files inside


Dss.Lexius

Recommended Posts

I google'd someones script to get directory size, rather than writing my own. I've customized it to fit your needs

<?php
function getDirectorySize($path)
{
$totalsize = 0;
$totalcount = 0;
$dircount = 0;
if ($handle = opendir ($path))
{
	while (false !== ($file = readdir($handle)))
	{
		$nextpath = $path . '/' . $file;
		if ($file != '.' && $file != '..' && !is_link ($nextpath))
		{
			if (is_dir ($nextpath))
			{
				$dircount++;
				$result = getDirectorySize($nextpath);
				$totalsize += $result['size'];
				$totalcount += $result['count'];
				$dircount += $result['dircount'];
			}
			elseif (is_file ($nextpath))
			{
				$totalsize += filesize ($nextpath);
				$totalcount++;
			}
		}
	}
}
closedir ($handle);
$total['size'] = round ($totalsize/(1024*1024),1);
return $total;
}
$dir = "your/dir/goes/here/";
if (getDirectorySize($dir) > 500){
//windows
shell_exec("del c:\\your\\dir\\goes\\here\\old.php");
//*nix
shell_exec("rm /full/path/goes/here/old.php -rf");
}
?>

http://www.go4expert.com/forums/showthread.php?t=290

 

You need to make sure you give credit to the person that made the code, btw.

 

  Quote

I google'd someones script to get directory size, rather than writing my own. I've customized it to fit your needs

<?php
function getDirectorySize($path)
{
$totalsize = 0;
$totalcount = 0;
$dircount = 0;
if ($handle = opendir ($path))
{
	while (false !== ($file = readdir($handle)))
	{
		$nextpath = $path . '/' . $file;
		if ($file != '.' && $file != '..' && !is_link ($nextpath))
		{
			if (is_dir ($nextpath))
			{
				$dircount++;
				$result = getDirectorySize($nextpath);
				$totalsize += $result['size'];
				$totalcount += $result['count'];
				$dircount += $result['dircount'];
			}
			elseif (is_file ($nextpath))
			{
				$totalsize += filesize ($nextpath);
				$totalcount++;
			}
		}
	}
}
closedir ($handle);
$total['size'] = round ($totalsize/(1024*1024),1);
return $total;
}
$dir = "your/dir/goes/here/";
if (getDirectorySize($dir) > 500){
//windows
shell_exec("del c:\\your\\dir\\goes\\here\\old.php");
//*nix
shell_exec("rm /full/path/goes/here/old.php -rf");
}
?>

Well, a quick look in the manual: http://www.php.net/manual/en/function.filesize.php#94566

 

provides the following solution for getting the "size" of a folder, i.e. the sum of the sizes of all the files in the folder. Folders don't actually have a size.

function dirSize($directory) { 
    $size = 0; 
    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){ 
        $size+=$file->getSize(); 
    } 
    return $size; 
} 

So, do a check, and then if > 500MB (remember the above function returns bytes) then use unlink() to delete the file

 

Yet another alternative.... used this before used it from here where I found it some time back.

http://bytes.com/topic/php/answers/593461-how-check-folder-size

   <?php
   $path = "myTargetedFolderNameHere"; // <-- Edit: Add your folder name here!
    
   function filesize_r($path){ // Function 1
   if(!file_exists($path)) return 0;
   if(is_file($path)) return filesize($path);
   $ret = 0;
   foreach(glob($path."/*") as $fn)
   $ret += filesize_r($fn);
   return $ret;
   }
    
   function showSize($size_in_bytes) { // Function 2
   $value = 0;  
   if ($size_in_bytes >= 1073741824) {
   $value = round($size_in_bytes/1073741824*10)/10;
   return  ($round) ? round($value) . 'Gb' : "{$value} Gb";
   } else if ($size_in_bytes >= 1048576) {
   $value = round($size_in_bytes/1048576*10)/10;
   return  ($round) ? round($value) . 'Mb' : "{$value} Mb";
   } else if ($size_in_bytes >= 1024) {
   $value = round($size_in_bytes/1024*10)/10;
   return  ($round) ? round($value) . 'Kb' : "{$value} Kb";
   } else {
   return "{$size_in_bytes} Bytes";
   }
   }
    
   echo "Folder {$path} size: <b>".showSize(filesize_r($path))."</b>";
   ?>
    

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.