Delete non empty folders with php

User avatar
JanMolendijk
Posts: 91
Location: Holland Rotterdam

Delete non empty folders with php

Dear Chef i`m back again, i try to do alot to get smarter...
This to make the website better but it is complicated
also my technical-english is not to well...

I found out how users can add a new personal folder for uploading pictures.
Now I want to expand it... I want to be able to delete folders,
& deleting folders who are not empty...

I readed many things about unlink but i did not succeed myself to complete

Please help me out... `thank you in advance` :roll:

Admin
Hello,
Try this function, which will delete a directory recursively (containing all its files and folders):

Code: Select all

function rmdir_all($dir) {
    foreach(scandir($dir) as $file) {
        if('.' === $file || '..' === $file) continue;
        if(is_dir("$dir/$file")) rmdir_all("$dir/$file");
        else unlink("$dir/$file");
    }
    rmdir($dir);
}

rmdir_all('some_dir/');

Or this:

Code: Select all

function rmdir_all($dir){
    $it = new RecursiveDirectoryIterator($dir);
    $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
    foreach($it as $file) {
        if('.' === $file->getBasename() || '..' ===  $file->getBasename()) continue;
        if($file->isDir()) rmdir($file->getPathname());
        else unlink($file->getPathname());
    }
    rmdir($dir);
}

rmdir_all('some_dir/');

JanMolendijk
Thanks for the Quick support Admin `just great`.
The second code is working but i still would like to Modernize it.
I want that users can type an folder-name, what they would like to delete & after submit that the specific folder delete from the server.

How can i do this?

Code: Select all

rmdir_all('images/'. $users['email'] .'/NewMap');

"/NewMap" must be Manually added (or some other name from a folder).

What do i do wrong into this code

Code: Select all

<?php
require('connection.php');
extract($_POST);

//image
$map ='map';
?>

<form method="post" enctype="multipart/form-data">
         <table class="table table-bordered">
   <Tr>
      <Td colspan="2"><?php echo @$err;?></Td>
   </Tr>
            <tr>
               <td>Name from your Folder you want to delete<br>
 </td>
               <Td><input type="text"  class="form-control" name="map" required/></td>

            </tr>
<Td colspan="2" align="center">
<input type="submit" class="btn btn-success" value="Save" name="save"/>
<input type="reset" class="btn btn-success" value="Reset"/>
</td>


Code: Select all

<?php
function rmdir_all($dir){
    $it = new RecursiveDirectoryIterator($dir);
    $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
    foreach($it as $file) {
        if('.' === $file->getBasename() || '..' ===  $file->getBasename()) continue;
        if($file->isDir()) rmdir($file->getPathname());
        else unlink($file->getPathname());
    }
    rmdir($dir);
}

rmdir_all('images/'. $users['email'] .'/$map');
?>
<?php echo $users['email'];?>

i try here with $map ='map'; to add manual the folder-name
but i getting a error because i just do it wrong:

Code: Select all

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(images/lesbian-community@hotmail.com/$map,images/lesbian-community@hotmail.com/$map): Het systeem kan het opgegeven bestand niet vinden. (code: 2)' in C:\xampp\htdocs\Comments\TESTING.php:59 Stack trace: #0 C:\xampp\htdocs\Comments\TESTING.php(59): RecursiveDirectoryIterator->__construct('images/lesbian-...') #1 C:\xampp\htdocs\Comments\TESTING.php(69): rmdir_all('images/lesbian-...') #2 {main} thrown in C:\xampp\htdocs\Comments\TESTING.php on line 59

Admin
Try access the delete rmdir_all() function like this:

Code: Select all

rmdir_all('images/'. $users['email'] .'/'.$map);

The "images/..." folder must be in the same directory as the php file for deleting (TESTING.php).
If it is in other location, make sure you put the correct path to folder you want to delete.

JanMolendijk
Sorry for the mash...
But i found out that it goes wrong here $map ='map';

Code: Select all

<?php
require('../connection.php');
extract($_POST);
//image
$map ='map';
?>


Is it posible to add manual a folder name what i want to delete
I used this code for it (but it is wrong).
it does not change the letters map

Code: Select all

$map ='map';

Code:

Code: Select all

<form method="post" enctype="multipart/form-data">
<table class="table table-bordered">
   <Tr>
<Td colspan="2"><?php echo @$err;?></Td>
</Tr>         
<tr>
<td>Name from your Folder you want to delete<br><?php echo $users['email'];?>
 </td>
<Td><input type="text"  class="form-control" name="map" required/></td>

</tr>
<Td colspan="2" align="center">
<input type="submit" class="btn btn-success" value="Save" name="save"/>
<input type="reset" class="btn btn-success" value="Reset"/>
</td>

Admin
If you want to get the value from a form field sent with: method="post", use: $_POST['field_name'] in php.

Code: Select all

$map = $_POST['map'];

JanMolendijk
I'm a little further but it delete directly the
whole folder without using first an submit button.

But i want to delete the folder only after hitting the submit-button

I do not hope I'm too bothered :roll:

Code: Select all

<?php
require('connection.php');
extract($_POST);

$folder = $_POST['folder'];

//folder
$map = $_POST['map'];

?>

<form method="post"><input type="text" name="map" value="">
<input type="text" name="folder" value="">
  <input type="submit" value="post"></form>



Code: Select all

<?php

function rmdir_all($dir){
    $it = new RecursiveDirectoryIterator($dir);
    $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
    foreach($it as $file) {
        if('.' === $file->getBasename() || '..' ===  $file->getBasename()) continue;
        if($file->isDir()) rmdir($file->getPathname());
        else unlink($file->getPathname());
    }
    rmdir($dir);
}

rmdir_all('images/'. $users['email'] .'/'. $folder .'/'.$map);

?>

<?php echo $users['email'];?>


O yes almost forgotten i getting two index-errors

Code: Select all

Notice: Undefined index: folder in C:\xampp\htdocs\Comments\TESTING.php on line 25

Notice: Undefined index: map in C:\xampp\htdocs\Comments\TESTING.php on line 28

Admin
Something like this:

Code: Select all

function rmdir_all($dir){
  $it = new RecursiveDirectoryIterator($dir);
  $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
  foreach($it as $file) {
    if('.' === $file->getBasename() || '..' ===  $file->getBasename()) continue;
    if($file->isDir()) rmdir($file->getPathname());
    else unlink($file->getPathname());
  }
  rmdir($dir);
}

//if data from form
if(isset($_POST['folder']) && isset($_POST['map'])){
  $folder = $_POST['folder'];
  $map = $_POST['map'];

  //delete
  rmdir_all('images/'. $users['email'] .'/'. $folder .'/'.$map);
}

JanMolendijk
Dear Admin thanks for the great support...

I have a question: Is their a solution to protect it, because when i keep the form-fields empty it deletes the whole folder.
I known this but for the users it needs protection.

Admin
In the if() condition check post data to have at least one character.

Code: Select all

//if data from form
if(isset($_POST['folder']) && isset($_POST['map']) && strlen($_POST['folder'])>0 && strlen($_POST['map'])>0){
  $folder = $_POST['folder'];
  $map = $_POST['map'];

  //delete
  rmdir_all('images/'. $users['email'] .'/'. $folder .'/'.$map);
}

- And, use the required="required" attribute in <input> tag.

Code: Select all

<form method="post">
<input type="text" name="folder" value="folder" required="required">
<input type="text" name="map" required="required">
  <input type="submit" value="Delete"></form>

JanMolendijk
Thank you so mutch it is settlet now

Adv. Posts: 01

Similar Topics