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

<?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

13 Responses so far »

  1. 1

    shasha said,

    I try it and it works…. thanks

    shasha

  2. 2

    rizal said,

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

    Putera Emas said,

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

  4. 4

    kece said,

    powerful stuph. surprisingly fast too

  5. 5

    Gede said,

    Thanks bro.. totally helpful

  6. 7

    shivkumar yadav said,

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

  7. 8

    Bart Huisman said,

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

  8. 9

    Bart Huisman said,

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

  9. 10

    Bart Huisman said,

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

  10. 11

    Trd said,

    Very helpful. Thanks a lot!!

  11. 12

    paradox said,

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

  12. 13

    Fero said,

    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.


Comment RSS · TrackBack URI

Say your words