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

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. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.