-3

Suppose that 'Demo,hello.php,Chrysanthemum.jpg' is an array of directories and files that i want to delete in which demo is a directory containing directories and files.The following piece of code gives me this error.

Warning: rmdir(Demo): Directory not empty in C:\xampp\htdocs\file.php on line 32

please help me debugging this problem.

<?php 
    $arr = explode(',' ,'Demo,hello.php,Chrysanthemum.jpg');
    $len = count($arr);
    $i = 0;
    $directory = array();
    /*while($i < $len){
        $arr[$i] = '../uploads/'.$arr[$i];
        $i++;
    }*/
    while(count($arr) > 0){
        $dir = array_pop($arr);
        if(is_dir($dir)){
            array_push($directory ,$dir);
            $dh = opendir($dir);
            while(($file = readdir($dh)) !== false){
                if($file != '.' && $file != '..'){
                    if(is_dir($dir.'/'.$file)){
                        array_push($arr ,$dir.'/'.$file);
                    }
                    else{
                        unlink($dir.'/'.$file);
                    }
                }
            }
        }
        else{
            unlink($dir);
        }
    }
    while($dir = array_pop($directory)){
        rmdir($dir);
    }

?>
  • 2
    _Attempts to remove the directory named by dirname. **The directory must be empty**, and the relevant permissions must permit this. A E_WARNING level error will be generated on failure._ from http://php.net/manual/en/function.rmdir.php . Possible duplicate of: http://stackoverflow.com/questions/3349753/delete-directory-with-files-in-it – briosheje Jul 06 '15 at 10:54
  • The error message sounds pretty clear to me... – JJJ Jul 06 '15 at 10:55
  • the problem is that when I searched the 'Demo' folder the folder was empty then why it is giving me this error – user3809584 Jul 06 '15 at 10:58

1 Answers1

0

system("rm -rf ".escapeshellarg($dir));

System Manual

Simple as single line and Execute very fast. What ever the file inside it doesn't matter

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85