2013-11-20 61 views
0

除removeIt之外的所有功能都有效(空白頁)。這裏是我的代碼波紋管:功能不想刪除文件夾

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

我有3個功能,使其工作:

  1. isEmpty:驗證該文件夾爲空
  2. emptyIt:空文件夾和子文件夾
  3. removeIt:刪除文件夾

任何提示?

+1

任何錯誤.....? – geedubb

+1

'emptyIt'應當在找到文件夾時遞歸調用自身,以防嵌套級別更高。 – Barmar

+0

@geedubb:不,沒有錯誤。 –

回答

0

嘗試這將刪除folder及其contentsubfolders

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

其中$dirfolder

+0

它就像一個魅力!謝謝 ! –

0

路徑也許你有一個用戶執行程序,不具備權限對文件夾進行更改,嘗試使用root執行它或給當前用戶授予權限,祝你好運。

0

嘗試,這將not刪除emptydirecotiresfrom 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); 
} 
}