Domanda

I was asked to create a cron job that deletes all files and folders from a folder (recursive) excluding some file extensions.

I have this code (which I found on the web):

function rrmdir($dir) {
    $structure = glob(rtrim($dir, "/").'/*');
    if (is_array($structure)) {
        foreach($structure as $file) {
            if (is_dir($file)) rrmdir($file);
            elseif (is_file($file)) unlink($file);
        }
    }
    rmdir($dir);
}

Which will remove ANYTHING from the folder specified, but as said, I need to add exception to in (all '.php' files should not be remove).

Please assume the following structure for folder:

FOLDER1
FOLDER2
FOLDER3
FILE1.ZIP
FILE2.ZIP
DONOTDELETE1.PHP
DONOTDELETE2.PHP

So, everything should be deleted, except the php files

Can anyone help me with this?

È stato utile?

Soluzione

this is modified function try with this. this will delete all file except *.php OR *.PHP files

function rrmdir($dir) {
    $structure = glob(rtrim($dir, "/").'/*');

    $rm_dir_flag = true;

    if (is_array($structure))
    {
        foreach($structure as $file) 
        {
            if (is_dir($file))
            {
                rrmdir($file);
            }
            else if(is_file($file))
            {
                $ext = substr($file, -4);
                if($ext==".php" || $ext==".PHP")
                {
                    $rm_dir_flag = false;
                }
                else
                {
                    unlink($file);
                }   
            }   
        }
    }

    if($rm_dir_flag)
    {
        rmdir($dir);
    }   
}

UPDATE 2:

if you want to protect file with particuar extension you can do this

rrmdir($your_directory, ".php");

//or

rrmdir($your_directory, ".pdf");

//or

rrmdir($your_directory, ".jpeg");

function rrmdir($dir, $protect_extension) {

       if(!is_dir($dir))
       {
             return;
       }
       $len = strlen($protect_extension)*(-1);


        $structure = glob(rtrim($dir, "/").'/*');

        $rm_dir_flag = true;

        if (is_array($structure))
        {
            foreach($structure as $file) 
            {
                if (is_dir($file))
                {
                    rrmdir($file, $protect_extension);
                }
                else if(is_file($file))
                {
                    $ext = substr($file, $len);
                    if($ext==$protect_extension || $ext==strtoupper($protect_extension))
                    {
                        $rm_dir_flag = false;
                    }
                    else
                    {
                        unlink($file);
                    }   
                }   
            }
        }

        if($rm_dir_flag)
        {
            rmdir($dir);
        }   
    }

Altri suggerimenti

Can you try this, Added if($ext!='php'){

    function rrmdir($dir) {
        $structure = glob(rtrim($dir, "/").'/*');
        if (is_array($structure)) {
            foreach($structure as $file) {
                if (is_dir($file)){
                    rrmdir($file);
                }elseif(is_file($file)){
                    $info = pathinfo($file);
                    $ext = strtolower($info['extension']);
                    if($ext!='php'){
                        unlink($file);
                    }
                }
            }
        }
        rmdir($dir);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top