Dss.Lexius Posted June 17, 2010 Share Posted June 17, 2010 hi, i want to get size of a Directory and delete a file called old.php when the directory's total size is larger than 500mb. how to do it? i am new to PHP trying to make my work easy using PHP help me plz... Thanx Quote Link to comment https://forums.phpfreaks.com/topic/205092-how-to-get-directoryfolder-size-and-delete-files-inside/ Share on other sites More sharing options...
jonsjava Posted June 17, 2010 Share Posted June 17, 2010 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/205092-how-to-get-directoryfolder-size-and-delete-files-inside/#findComment-1073591 Share on other sites More sharing options...
kratsg Posted June 17, 2010 Share Posted June 17, 2010 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. 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/205092-how-to-get-directoryfolder-size-and-delete-files-inside/#findComment-1073593 Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/205092-how-to-get-directoryfolder-size-and-delete-files-inside/#findComment-1073597 Share on other sites More sharing options...
gwolgamott Posted June 17, 2010 Share Posted June 17, 2010 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/205092-how-to-get-directoryfolder-size-and-delete-files-inside/#findComment-1073601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.