Frage

All functions works except the removeIt one (blank page). Here is my code bellow :

class Dir {
    public function emptyIt($path) { 
        if ($handle = opendir($path)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if(is_file($path."/".$file)) {
                        unlink($path."/".$file);
                    } else {
                        if($handle2 = opendir($path."/".$file)) {
                            while (false !== ($file2 = readdir($handle2))) {
                                if ($file2 != "." && $file2 != "..") {
                                    unlink($path."/".$file."/".$file2);
                                }
                            }
                        }
                        rmdir($path."/".$file);
                    }
                }
            }
        }
        return true;
    }

    function isEmpty($path) {
        $handle=opendir($path);
        $i=0;
        while (false !== ($file = readdir($handle))) {
            $i++;
        }
        closedir($handle); 
        if($i>=2) {
            return false;
        } else {
            return true;
        }
    }

    public function removeIt($path) {
        if (emptyIt($path)) {
            if (rmdir($path)) {
                return true;
            } else {
                return false;
            }
        }
    }
}

I have 3 functions to make it work :

  1. isEmpty : verify if the folder is empty
  2. emptyIt : empty folder and subfolders
  3. removeIt : remove folder

Any hint ?

War es hilfreich?

Lösung

try this will remove the folder and its content (subfolders)

system('/bin/rm -rf ' . escapeshellarg($dir));

where $dir is the path of folder

Andere Tipps

Maybe you execute the program with a user that doesn't have the permissions to make changes on the folder, try executing it with root or give permissions to the current user, good luck.

try this will not remove the empty direcotires from php.net

function rrmdir($dir) {
if (is_dir($dir)) {
 $objects = scandir($dir);
 foreach ($objects as $object) {
   if ($object != "." && $object != "..") {
     if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
   }
 }
 reset($objects);
 rmdir($dir);
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top