Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recursively delete small files PHP script

    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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,497
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 Code:
    <?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;
    }

    ?>


    .
    Last edited by mlseim; 11-11-2011 at 07:20 PM.

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    86
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Another way if I might add:
    PHP Code:
    <?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");
    ?>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •