문제

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 ?

도움이 되었습니까?

해결책

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

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

where $dir is the path of folder

다른 팁

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);
}
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top