2010-02-05 70 views
7

我該怎麼做?有沒有kohana 3提供的方法?PHP刪除目錄的內容

+1

什麼是kohona 3? – Moshe 2010-02-05 05:18:27

+1

這是一個PHP框架,儘管這個問題與它沒有任何關係。 – 2010-02-05 17:56:48

回答

9

要刪除一個目錄及其所有內容,您必須編寫一些遞歸刪除函數 - 或使用已存在的函數。

您可以在rmdir文檔頁面的用戶註釋中找到一些示例;例如,這裏的the one proposed by bcairns在2009年8月(引用)

<?php 
// ensure $dir ends with a slash 
function delTree($dir) { 
    $files = glob($dir . '*', GLOB_MARK); 
    foreach($files as $file){ 
     if(substr($file, -1) == '/') 
      delTree($file); 
     else 
      unlink($file); 
    } 
    rmdir($dir); 
} 
?> 
+0

$ files = glob($ dir。'*',GLOB_MARK); - >請解釋這一行。謝謝。 – 2010-02-05 05:24:58

+0

'glob'函數*(請參閱http://php.net/glob)*將返回與模式匹配的所有文件,而'*'將匹配所有文件,這意味着glob將返回所有文件的列表'$ dir'指向的目錄;; 'GLOB_MARK'意思是「*爲每個返回的目錄添加一個斜槓*」 – 2010-02-05 05:34:47

+2

如果你想刪除文件夾的內容 - 不是文件夾本身 - 刪除'rmdir($ dir);'行 – PaulSkinner 2013-01-11 16:11:50

0

你嘗試過在目錄中取消鏈接?

 chdir("file"); 
    foreach (glob("N*") as $filename) 
     { 
     unlink($filename); 
     } 

這將刪除文件名從N個

0

開始我不知道有關的Kohana 3,但我會結合使用DirectoryIterator()unlink()

4

我建議這樣,簡單直接。

$files = glob('your/folder/' . '*', GLOB_MARK); 
    foreach($files as $file) 
    { 
     if (is_dir($file)) { 
      self::deleteDir($file); 
     } else { 
      unlink($file); 
     } 
    }