<?php
/**
* Delete a file, or a folder and its contents
*
* @author Aidan Lister <aidan@php.net>
* @version 1.0.2
* @param string $dirname Directory to delete
* @return bool Returns TRUE on success, FALSE on failure
*/
function rmdirr($dirname)
{
// Sanity check
if (!file_exists($dirname)) {
return false;
}
// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == ‘.’ || $entry == ‘..’) {
continue;
}
// Recurse
rmdirr(“$dirname/$entry”);
}
// Clean up
$dir->close();
return rmdir($dirname);
}
?>
I try it and it works…. thanks
shasha
i tried but it not work
at first i thought it will only delete files in folder specified. But it delete all files and folder AND ALSO FOLDER YOU USE FOR THE FUNCTION!
@@”
But this is what i need. Although a bit dangerous..
To play safe i use like this:
if (file_exists($dir_folder_to_delete)) {
rmdirr($dir_folder_to_delete);
}
oppp.. sorry to warn you. it will completely delete the contents in the folder.
powerful stuph. surprisingly fast too
Thanks bro.. totally helpful
cool.
i want to know is there any function to check folder is empy or not
i use it this way:
function rmdirr($dirname){
// Sanity check
$dirname = “../albums/” . $dirname;
if (!file_exists($dirname)) {
return false;
}
// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == ‘.’ || $entry == ‘..’) {
continue;
}
// Recurse
unlink(“$dirname/$entry”);
}
// Clean up
$dir->close();
return rmdir($dirname);
}
if (rmdirr($_GET[‘map’])){
echo ”;
}
else{
echo something went wrong.’;
}
changes i made: other folder, could be removed if you want.
also the quotes are wrong when i copy it from here, it has to be straight quotes, no “diagonal” quotes (sorry, dont know a better english explenation)
also the loop when checking if there ale files in the flder, to delete the files, i changed it in unlink, becouse with the original he did not deleted the files, only give a php error the folder is nog empty….
hmm, i see now my quotes are wrong now too, so you have to check if the quotes (single and double) are the good ones for php, not the “diagonal” ones.
ok, just a test to see if you could see the difrence between the quotes:
” , ‘ , ` , ” , ‘ , ’
ok, theres used an bad script to avoid hacking i think, all straight qoutes are changed is diagonal ones, and not set back when showing here, so you just have to chenge the quotes by yourself….
Very helpful. Thanks a lot!!
@ Bart Huisman; Very nice. Where you have this script hosted? You make it very easy for me to deface your entire website.
Its working well and good. But if i need to delete the folder which contains some files and empty folder. In this case it delete all the files but not the empty folder. It throws an exception like…
Warning: unlink(TEST//New Folder) [function.unlink]: Permission denied in E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 23
Warning: rmdir(TEST/) [function.rmdir]: Directory not empty in E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 28
What is the possible way to delete even the folder is empty.
function delete_directory($dirname)
{
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != “.” && $file != “..”) {
if (!is_dir($dirname.”/”.$file))
unlink($dirname.”/”.$file);
else
delete_directory($dirname.’/’.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
I want to delete only content of a file, the above coding is a bit confusing …
hiii great code, so helpfull, thanks mate
the code ROCKSSSSSSS !!!!!!!
Hey,
here is simple and 100% working code
$dir = ‘your/directory/’;
foreach(glob($dir.’*.*’) as $v){
unlink($v);
}
hope you will like it
plz reply
Hey,
why do you have there in the code the part which starts “sanity check”? If I understand it, it returns “false” every time.
perfect. it worked for me. thanks heaps dude.
Hey do you have a code that delete only one file from the folder plz…
I have many checkboxes and when checked it should delete only one file at a time .
But when I use the code below, all the files are deleted at once. any help would me much appreciated .
Here is my code how it looks like:
<?php
$checkbox = @$_POST['checkbox'];
$delete = @$_POST['delete'];
$dir = "music/";
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){ //loop for checkbox
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE songid='$del_id'";
$result = @mysql_query($sql);
}
//unlinking files
foreach(glob($dir."*.*") as $del_id){
unlink($del_id);
}
// if successful redirect to delete_multiple.php
if(@$result){
echo "”;
}
}
@mysql_close();
?>