...

View Full Version : Recursively delete small files PHP script



jole1234
11-11-2011, 04:50 PM
How to recursively delete small files, smaller than 10 kilobytes regardless on their creation time, format or name?

PHP script needs to go through cache folder and all sub folders and delete files smaller than 10 KB, but to leave all larger files. It would be nice to show list of deleted files, but it is not must. Also it would be nice if script can be set via cron job, but also it is not must, it can do manually too.

P.S. I am not programmer, so I need already made script or at least pretty much made

Best regards.

mlseim
11-11-2011, 05:07 PM
Before you run a script that will delete files, you can try the
script below to verify that it's picking-out the correct files.
Now, where you put this script might be important, because it
will see itself (as it's less than 10KB).

So, put this script in a directory other than the ones you are scanning.
Adjust the $root value accordingly.

Here is the script for you to test out:


<?PHP

// This uses a function found in the readdir PHP manual.

// Only show files less than this number of bytes ...
$bytes=10000;

// In your example, I think you need to put this script one directory below "cache".
// $root = "./cache";
// Where to start scanning - the default setting is the root.
//$root = ".";
$root = "./cache";

$files=array();
$files=read_all_files($root);

foreach($files['files'] as $file){
$size=explode("|",$file);
if($size[1]<$bytes){
echo $size[0]." &nbsp; size:".$size[1]."<br />";
}
}

/**
* Finds path, relative to the given root folder, of all files and directories in the given directory and its sub-directories non recursively.
* Will return an array of the form
* array(
* 'files' => [],
* 'dirs' => [],
* )
* @author sreekumar
* @param string $root
* @result array
*/
function read_all_files($root){
$files = array('files'=>array(), 'dirs'=>array());
$directories = array();
$last_letter = $root[strlen($root)-1];
$root = ($last_letter == '\\' || $last_letter == '/') ? $root : $root.DIRECTORY_SEPARATOR;

$directories[] = $root;

while (sizeof($directories)) {
$dir = array_pop($directories);
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..') {
continue;
}
$file = $dir.$file;
if (is_dir($file)) {
$directory_path = $file.DIRECTORY_SEPARATOR;
array_push($directories, $directory_path);
$files['dirs'][] = $directory_path;
} elseif (is_file($file)) {
$size=filesize($file);
$files['files'][] = $file."|".$size;
}
}
closedir($handle);
}
}

return $files;
}

?>




.

Jinxy
11-13-2011, 05:05 PM
Another way if I might add:

<?php
function deleteFile($dir) {
$dhandle = opendir($dir);
if ($dhandle) {
while (false !== ($fname = readdir($dhandle))) {
if (is_dir( "{$dir}/{$fname}" )) {
if (($fname != '.') && ($fname != '..')) {
echo "<u>Deleting Files in the Directory</u>: {$dir}/{$fname} <br />";
deleteFile("$dir/$fname");
}
} else {
if (filesize("{$dir}/{$fname}") < '10000') {
echo "Deleting File: {$dir}/{$fname} <br />";
unlink("{$dir}/{$fname}");
}
}
}
closedir($dhandle);
}
}
deleteFile("/home/user/public_html/cache");
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum