Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
Aidan's PHP Repository / function.rmdirr.php
[go: Go Back, main page]

Aidan's PHP Repository

A repository for PHP functions and classes ...

function.rmdirr.php

Delete a file, or a folder and its contents

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.0.3
  • Link: http://aidanlister.com/repos/v/function.rmdirr.php
  • Return: bool Returns TRUE on success, FALSE on failure
  • Views: 29271
  • Downloads: 2581

Source

Download this script <?php
/**
 * Delete a file, or a folder and its contents
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.3
 * @link        http://aidanlister.com/repos/v/function.rmdirr.php
 * @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) || is_link($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 . DIRECTORY_SEPARATOR . $entry);
    }
 
    // Clean up
    $dir->close();
    return rmdir($dirname);
}
 
?>

Comments

July 6th, 2006
Works like a charm, thanks
May 23rd, 2006
gracias Aidan!! I use your function in my PHP module: http://www.viciao2k3.net/modules.php?name=ftp2nuke thx a lot !! :)
March 22nd, 2006
Thanks! Keep up the great work, Aidan.
December 29th, 2005
Hey its a very good script. Thanks
December 26th, 2005
Great script. Had an install go crazy leaving a directory owned by the webserver. I was pulling my hair out. Thanks.
December 2nd, 2005
Simply Beautiful
November 7th, 2005
QUALITY. THANKS!
October 20th, 2005
Perfect, just what I needed, thanks.
October 20th, 2005
Its just excellent.
August 4th, 2005
my attempt to make this great function a little better... read())) { if ('.' == $entry2 || '..' == $entry2) { continue; } $result = self::rmr("$entry/$entry2") ? $result : false; } $dir->close(); $result = @rmdir($entry) ? $result : false; } else { // failed to open directory return false; } } return $pass && $result; } ?>
July 11th, 2005
finaly a good rmdir function, thx!
July 8th, 2005
Muchas gracias por el script me ayudo mucho
June 29th, 2005
*king Excellent
May 20th, 2005
WAOW! Worked perfectly first time. Genius.
May 19th, 2005
good work ! -- excellent -- ;)
May 11th, 2005
Nice script. was looking for something like this for a few days now. Thx
May 9th, 2005
thanks
May 8th, 2005
nice work, cheers matey, just flipped over to the php world from jsp. It seems php is supported more than jsp by the web hosts out there, so I had to do it. :) Sorry asp, didn't really give you a thought :(
May 5th, 2005
thanks, good work
May 2nd, 2005
Thank you :)
April 27th, 2005
good
April 16th, 2005
btw, should'nt you do the same is_link() condition for the copyr() function ?! :] daita.
April 16th, 2005
Bug with a symlink on a directory: it remove all files of the destination dir of the symlink. Please add: if (is_link($dirname)) return unlink($dirname); after (or before) the is_file condition ;) btw, cool script; thanks. daita. [Editor's Note: Fixed!]
April 13th, 2005
Tanks!
April 9th, 2005
Everytime useful man u saved my life i coded with the most of functions my site lol size_readable dir_size and shit like that verry nice to make something like this
March 29th, 2005
Thank you, very useful and simply!
March 29th, 2005
Great script. Just what I was looking for! Thanks
March 14th, 2005
thanks
March 14th, 2005
U SAVE MY LIFEEEE!!!!
March 12th, 2005
Very helpful, saved me a lot of grief when I was haveing 550: permission denied error's.
March 12th, 2005
its simply suberb.......... thankx, keep it up
March 8th, 2005
Thanx. This script helps me a LOT.
March 6th, 2005
thanx a lot man...
March 3rd, 2005
Just look out for symlinks in linux/unix - tis script will follow symlinks to another folder and start emptying that too. To avoid this change: if (is_file($dirname)) { to: if (is_file($dirname) || is_link($dirname)) { [Editor's Note: Ouch, that could be nasty. I've fixed this, Thanks.]
March 1st, 2005
Thank you for this script! It really helped me!
March 1st, 2005
muito bom esse script, salvou minha vida!!!
February 25th, 2005
First of all, great function. I've got a small problem going on though. Lets say I have a directory with two subdirectories. If I am browsing the main directory with Windows Explorer, and run the function it will go perfectly. Now if I am browsing one of the subdirectories and run the script, it will empty everything out from the main directory but does not delete it alleging that it is not empty. Cheers, great job! [Editor's Note: This is windows again. It locks a directory when you are viewing it with windows explorer, it's a pain in the butt.]
February 24th, 2005
thanks very much, just what i needed
February 22nd, 2005
Thanks!
February 19th, 2005
Cool! Thx
February 19th, 2005
why the heck isn't this built in to PHP? They have 10 sort functions but no 'rm -rf' function.
February 18th, 2005
basic and useful ;)
February 18th, 2005
juste great ... thx ;)
February 18th, 2005
Thank
February 16th, 2005
thank you man , good work
February 13th, 2005
It`s a fantastic php`s script! Thank !
January 28th, 2005
thankyou v muchly !
January 28th, 2005
Thanks a lot for your script ! Very usefull.
January 26th, 2005
With this script I had the "Permission-denied" problems. I'd try to add umask function to resolve: close(); umask (0755); return rmdir($dirname); ?>
January 25th, 2005
Thx for your function, very good! I bookmarked your site..very nice function and classes..
January 15th, 2005
Not to be a wise guy, but.... How do you use this?? I have directories that contain images and it would be sweet if they can be vaporized at the same time that I delete the users from the db who uploaded the images in the first place :( (reading the comments this seems useful but I need HELLLP!) [Editor's Note: You can use this the same way you'd use any other function...
January 11th, 2005
Thanks a lot:-)
January 7th, 2005
Thank you very much! Your work is wonderfull.
January 3rd, 2005
thanks~!!!!!!!!
January 1st, 2005
this is a super job! thanks!!!
December 21st, 2004
I had to delete around 25,000 directories and 100,000 files from a site. The FTP program was going to take several days. With this, modifying the code, testing and deleting was less than an hour. Thanks a lot.
December 3rd, 2004
Good job for the programmer
November 26th, 2004
Very nice - I never knew about $dir before. Anyhow, I wrote my own function similar (not using $dir) and it's virtually the same as yours except I noticed that (when making mine), the entire "Delete deep directories" thing is unnecessary - all you need to do is call rmdirr() there - no is_dir test is required. The reason for this is the first thing rmdirr() does is test for a plain file and unlink it, so why put 2 unlinks when all you need is 1 - save some lines of code, just recurse the rmdirr() function and have the 2nd call to it unlink the file. [Editor's Note: Ah, I overlooked that, I've updated the code]
November 24th, 2004
Perfect, both this script and the mkdir script. Unbelievable, exactly what i needed. Thank you
November 20th, 2004
yes, this helps a lot... unlink should take wildcards and be recursive if desired, but the dangers of it are obvious... this routine does the job well :-)
November 5th, 2004
Very useful script. Thanks!
November 5th, 2004
Thanx..
November 5th, 2004
nice! Ty.
November 5th, 2004
nice
November 5th, 2004
thanks man :)
November 5th, 2004
Just what I needed, Thanx
November 5th, 2004
good work, thank a lot
November 5th, 2004
thanks man for the script.
November 5th, 2004
thanks for saving me the time of rewriting that :)
November 5th, 2004
this script is very very useful.. just wat i need...
November 5th, 2004
Thanks a lot!
November 5th, 2004
great post
November 5th, 2004
Man, that script is a must! Thanks a lot
November 5th, 2004
Nice! Thanks, this was what I needed!
November 5th, 2004
thank you!!!
November 5th, 2004
Thanks, very useful script!
November 5th, 2004
Very nice.
November 5th, 2004
Thanks for such good script! Very interesting. I wrote similar, but have several problems, such as: script shows me error that access is denied (when it must delete directory(rmdir), but permission was 0777).
November 5th, 2004
Had the same "permission denied"-problem when I tried to delete a folder just after it was closed by "dir->close();". But it works if you call that function twice ( i.e. "dir->close(); dir->close();") (php version 4.2.1, Win XP, IIS) [Editor's Note: WinXP has some file locking issues, especially on slower machines. If you add a usleep(10) above the unlink() function, it should resolve this (but also slow down your script). If you're not using a slow computer, don't worry about it.]
November 5th, 2004
thank a lot too!!
November 5th, 2004
Thank 's a lot Before i used your script i'd having an error message : permission denied... :( Now with your function yahoooo it runs like i want : )
November 5th, 2004
AWESOME. Thanks.
November 5th, 2004
Thanks for your help... Now I will have less code to write... :D
November 5th, 2004
I added this before the first "unlink" to avoid parse errors: if (!file_exists($dirname)) return false; [Editor's Note: Good point, I added this in now]
November 5th, 2004
Hey - just what I needed with a sleepy can't be bothered brain. Thanks very much |-)
November 5th, 2004
very useful script. thanks