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`
Delete non empty folders with php
-
- Posts:282
- Location:Holland Rotterdam
Delete non empty folders with php
Admin
Posts:805
Hello,
Try this function, which will delete a directory recursively (containing all its files and folders):
Or this:
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/');
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
Posts:282
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?
"/NewMap" must be Manually added (or some other name from a folder).
What do i do wrong into this code
i try here with $map ='map'; to add manual the folder-name
but i getting a error because i just do it wrong:
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');
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'];?>
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
Posts:805
Try access the delete rmdir_all() function like this:
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.
Code: Select all
rmdir_all('images/'. $users['email'] .'/'.$map);
If it is in other location, make sure you put the correct path to folder you want to delete.
JanMolendijk
Posts:282
Sorry for the mash...
But i found out that it goes wrong here $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:
But i found out that it goes wrong here $map ='map';
Code: Select all
<?php
require('../connection.php');
extract($_POST);
//image
$map ='map';
?>
I used this code for it (but it is wrong).
it does not change the letters map
Code: Select all
$map ='map';
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
Posts:805
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
Posts:282
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
O yes almost forgotten i getting two index-errors
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
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'];?>
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
Posts:805
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
Posts:282
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.
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
Posts:805
In the if() condition check post data to have at least one character.
- And, use the required="required" attribute in <input> tag.
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);
}
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
Posts:282
Thank you so mutch it is settlet now
Similar Topics
- GET_id not working in UnLink (delete file)
PHP - MySQL First post
I searching for an hour for a solution; unlink seems not to work with GET idLast post
<?php
$id = (int) $_GET ;
echo $_GET ;
$file = fopen( '.$_GET...
Here is an answer `o god after 2 hours shame on me for this one`
<?php
$file_pointer = $_GET .'.txt';
if (!unlink($file_pointer)) {
echo (...