List Folders and delete files within them at once

Asked

Viewed 217 times

0

My idea is that I could delete all the files inside the folders at once or per period (for example, every 5 months list me these files within the period).

I managed to list all the folders and by clicking on them I can view the files.

Someone could heal with some example to continue from the PHP code below?

EDIT: Basically, it would list all the folders and delete all the files inside the existing folders at once.

Thank you in advance!

CODE:

<?php
    $dir = "./";
    foreach (glob ($dir."*", GLOB_ONLYDIR) as $pastas) {
        if (is_dir ($pastas)) {
        echo "Motorista: ";
            echo str_replace ( $dir,"",$pastas)." <img style='width: 20px; height: 20px;' src=\"icon-motorista.png\"/> - <a href=\"$pastas\">Olhar pasta</a><br />";
                        echo "";
        }
    }
    ?>

2 answers

1


That’s pretty simple you just need to use another glob() within the first loop, to create another loop and delete all files from the subfolders.

## lista as pastas do diretorio atual
foreach(glob('./*', GLOB_ONLYDIR) as $i){
    ## lista e apaga todos os arquivos das subpastas
    foreach(glob($i.'/*') as $e){
        unlink($e);
    }
}

Edit:

$date = "27-4-2019";

## checa a data
if(date("d-m-Y") == date("d-m-Y", strtotime($date.'+5 Month')));
    foreach(glob('./*', GLOB_ONLYDIR) as $i){
        foreach(glob($i.'/*') as $e){
            unlink($e);
        }
    }
}
  • Parse error: syntax error, Unexpected 'foreach' (T_FOREACH) in C: xampp htdocs chametaxi driver admin uploads test.php on line 5 gives this error, but how could I put it functional in that code above?

  • I edited the code, but it’s the same thing, you said you wanted to delete all the files from the subfolders, and that’s what the above code will do, so pq vc would want to give echo to the folder link?

  • It was more to make sure that the folders would be "created" yet. Thanks for the help! Still in this code, is it possible to put time with Alert to delete the files? Ex: I opened the page and gives me an Alert "Only open this link after 5 months" and put a date condition for when to open again not to delete the files but after the specified date?

  • Yes, it’s possible, I edited the code. Ma regarding the alert() you have to give a search on ajax or javascript to do all this.

  • The date is fixed right? so I would have to keep editing this fixed date for the current day to delete? theoretically that prevents if open again, does not exclude?

  • you only have to change the value of the variable $data for the day you want me to start counting (current day + 5 months).

Show 1 more comment

0

Try this:

<?php
    function excluir($dir){
        if(is_file($dir)){
            return unlink($dir);
        }elseif(is_dir($dir)){
            $files = array_diff(scandir($dir), array('.','..'));
            foreach ($files as $file){
                (is_dir("$dir/$file")) ? excluir("$dir/$file") : unlink("$dir/$file");
            };
            //return rmdir($dir); //remova caso queira apaga a pasta
        };
    };
    excluir("./"); //coloque aqui o diretório que deseja excluir.
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.