PHP : Delete a file, or a folder and its contents

27 Feb

<?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);
}

?>

http://aidan.dotgeek.org/lib/?file=function.rmdirr.php

22 Responses to “PHP : Delete a file, or a folder and its contents”

  1. shasha November 27, 2006 at 10:41 pm #

    I try it and it works…. thanks

    shasha

    • rajesh March 31, 2013 at 1:35 pm #

      i tried but it not work

  2. rizal December 17, 2006 at 2:25 am #

    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);
    }

  3. Putera Emas December 20, 2006 at 7:51 pm #

    oppp.. sorry to warn you. it will completely delete the contents in the folder.

  4. kece May 3, 2007 at 9:40 pm #

    powerful stuph. surprisingly fast too

  5. Gede July 14, 2008 at 8:47 pm #

    Thanks bro.. totally helpful

  6. www.fimizone.org November 19, 2008 at 5:52 pm #

    cool.

  7. shivkumar yadav March 10, 2009 at 6:03 pm #

    i want to know is there any function to check folder is empy or not

  8. Bart Huisman April 21, 2009 at 6:16 pm #

    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….

  9. Bart Huisman April 21, 2009 at 6:19 pm #

    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:
    ” , ‘ , ` , ” , ‘ , ’

  10. Bart Huisman April 21, 2009 at 6:22 pm #

    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….

  11. Trd May 6, 2009 at 4:35 pm #

    Very helpful. Thanks a lot!!

  12. paradox July 3, 2009 at 2:31 am #

    @ Bart Huisman; Very nice. Where you have this script hosted? You make it very easy for me to deface your entire website.

  13. Fero September 17, 2009 at 2:29 pm #

    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.

  14. Vasim Padhiyar April 3, 2010 at 3:32 pm #

    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;
    }

  15. Zabi March 2, 2011 at 10:07 pm #

    I want to delete only content of a file, the above coding is a bit confusing …

  16. kuink September 15, 2011 at 7:53 pm #

    hiii great code, so helpfull, thanks mate

  17. Bittu October 24, 2011 at 12:23 am #

    the code ROCKSSSSSSS !!!!!!!

  18. SimpleDude October 24, 2011 at 12:29 am #

    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

  19. bla March 25, 2012 at 5:58 am #

    Hey,
    why do you have there in the code the part which starts “sanity check”? If I understand it, it returns “false” every time.

  20. Channa Abeetha Bandara March 28, 2012 at 2:35 am #

    perfect. it worked for me. thanks heaps dude.

  21. Nigel May 12, 2012 at 4:07 pm #

    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();
    ?>

Leave a reply to shasha Cancel reply