"Kioto" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi all I have create this function to delete directory and file but i
> have no result .
> When i call the function i don't receive any errors by PHP.
> I have test this function with PHP 4.3.6 and Apache for NT version 1.3.1
> on Windows Xp Professional
> I write this code :
> <?php
>
> error_reporting(E_ALL);
>
> function DropDir($dir) {
>
>  if (is_dir($dir)) {
>    if (!($handle = opendir($dir)))
>       die("It' no possible open directory  $dir");
>
>  while(false !== ($file = readdir($handle))) {
>     if ($file != '.' && $file != '..') {
>       if (is_dir($dir . '/' . $file)) {
>        DropDir($dir . '/' . $file);
>        exec("rmdir $file");
>       }
>       else {
>        exec("del $file");
>       }
>     }
>   }
>   closedir($handle);
>   exec("rmdir $dir");
>  }
> }
> file://call function DropDir
> DropDir('my_folder');
> ?>
> I think that it' impossible for me find solution to this problem.
> Can you help me ?
> Thanks so much to all an sorry for my bad language

I guess you'll first have to delete all files in the directory. Try this
function:


    /**
     * Recursively clear a local directory
     *
     * @param string  $sourceDir    directory to delete
     * @param integer $level        directory level (to avoid deleting the
root dir)
     */
    function clearDirectory($sourceDir, $level = 0)
    {
        // proceed if source directory exists
        if  (is_dir($sourceDir))
            {
                // read dir contents
                if  ($handle = opendir($sourceDir))
                    {
                           /* This is the correct way to loop over the
directory. */
                           while(false !== ($dirItem = readdir($handle)))
                                {
                                    if  ($dirItem != '.' && $dirItem !=
'..')
                                        {
                                            // directory
                                            if      (is_dir($sourceDir . '/'
. $dirItem))
                                                    {

clearDirectory($sourceDir . '/' . $dirItem, $level + 1);
                                                    }
                                            // file
                                            elseif  (file_exists($sourceDir
. '/' . $dirItem))
                                                    {
                                                        unlink($sourceDir .
'/' . $dirItem);
                                                    }
                                        }
                                }

                        // remove directory if it's not the root one
                        if  ($level > 0)
                            {
                                rmdir($sourceDir);
                            }
                    }

                    closedir($handle);
            }
    }

It will delete all direcotries and files *inside* the specified directory.
So the directory itself will not be deleted.

Just call it like this (without trailing slash!!!):

clearDirectory('/path/to/directory'); // or on windows
clearDirectory('c:\path\to\directory');

Hope it works for you.

Regards, Torsten Roehr

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to