Jump to content

[SOLVED] remove folder contents


Harley1979

Recommended Posts

 

 

<?php
    function full_rmdir( $dir )
    {
        if ( !is_writable( $dir ) )
        {
            if ( !@chmod( $dir, 0777 ) )
            {
                return FALSE;
            }
        }
       
        $d = dir( $dir );
        while ( FALSE !== ( $entry = $d->read() ) )
        {
            if ( $entry == '.' || $entry == '..' )
            {
                continue;
            }
            $entry = $dir . '/' . $entry;
            if ( is_dir( $entry ) )
            {
                if ( !$this->full_rmdir( $entry ) )
                {
                    return FALSE;
                }
                continue;
            }
            if ( !@unlink( $entry ) )
            {
                $d->close();
                return FALSE;
            }
        }
       
        $d->close();
       
        rmdir( $dir );
       
        return TRUE;
    }
?>

Link to comment
Share on other sites

Like you said you have to detect every single file and delete them one by one....

 

Take a good look at these functions:

 

http://no.php.net/opendir

http://no.php.net/readdir

http://no.php.net/unlink

 

As the example in readdir() you simply loop over all the files and each time you have a file you use unlink() to delete it.

 

EDIT: Or just use the code MadTechie posted - it's about perfect. Because there is alot more to it than taking one file and deleting it - what if it's an directory and you need to delete all that directories files before you can delete the dir ect. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.