2012-09-27 62 views
2

我使用php來刪除包含刪除的帖子圖像的文件夾。我使用的是我在網上找到的代碼,並且做得很好。php刪除一個特定的文件夾及其所有內容

我想知道如何在文件夾中存在其他文件夾時只刪除特定文件夾。

當我使用下面的代碼時,怎麼可能做到這一點? 使用是:/ dev /圖片/諾曼/ 8 - >不會刪除文件夾8 使用是:/ dev /圖片/諾曼/ - >將刪除所有文件夾

Eg: 
/dev/images/norman/8 -> I need to delete only this folder 
/dev/images/norman/9 
/dev/images/norman/10 
/dev/images/norman/11 
/dev/images/norman/12 

<?php 
$path = $_SERVER['DOCUMENT_ROOT'].'/dev/images/norman/8'; 

emptyDir($path); 

function emptyDir($path) { 

    // INITIALIZE THE DEBUG STRING 
    $debugStr = ''; 
    $debugStr .= "Deleting Contents Of: $path<br /><br />"; 

    // PARSE THE FOLDER 
    if ($handle = opendir($path)) { 

     while (false !== ($file = readdir($handle))) { 

      if ($file != "." && $file != "..") { 

       // IF IT"S A FILE THEN DELETE IT 
       if(is_file($path."/".$file)) { 

        if(unlink($path."/".$file)) { 
        $debugStr .= "Deleted File: ".$file."<br />"; 
        } 

       } else { 

        // IT IS A DIRECTORY 
        // CRAWL THROUGH THE DIRECTORY AND DELETE IT'S CONTENTS 

        if($handle2 = opendir($path."/".$file)) { 

         while (false !== ($file2 = readdir($handle2))) { 

          if ($file2 != "." && $file2 != "..") { 
           if(unlink($path."/".$file."/".$file2)) { 
           $debugStr .= "Deleted File: $file/$file2<br />";  
           } 
          } 

         } 

        } 

        if(rmdir($path."/".$file)) { 
        $debugStr .= "Directory: ".$file."<br />"; 
        } 

       } 

      } 

     } 

    } 
    echo $debugStr; 
} 

?> 
+0

嘗試爲/ dev /圖片/諾曼/ 8/ – FirmView

+0

@FirmView這是刪除文件夾下的8一切,但不是文件夾8 – Norman

+0

你有沒有試過我發佈的解決方案? – FirmView

回答

5
<?php 

delete_directory($dirname) { 
    if (is_dir($dirname)) 
     $dir_handle = opendir($dirname); 
    if (!$dir_handle) 
     return false; 
    while($file = readdir($dir_handle)) { 
     if ($file != "." && $file != "..") { 
     if (!is_dir($dirname."/".$file)) 
      unlink($dirname."/".$file); 
     else 
      delete_directory($dirname.'/'.$file);  
     } 
    } 
    closedir($dir_handle); 
    rmdir($dirname); 
    return true; 
} 
?> 

如果你正在使用,版本5.1及以上,

<?php 
function deleteDir($dir) { 
    $iterator = new RecursiveDirectoryIterator($dir); 
    foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) 
    { 
     if ($file->isDir()) { 
     rmdir($file->getPathname()); 
     } else { 
     unlink($file->getPathname()); 
     } 
    } 
    rmdir($dir); 
} 

deleteDir("temporary"); 
?> 
+0

首先該文件夾的所有內容將被刪除。最後,該文件夾將被刪除。 – FirmView

2

你想聽rmdir

if(is_file($path."/".$file)) { 

    if(unlink($path."/".$file)) { 
    $debugStr .= "Deleted File: ".$file."<br />"; 
    } 

} else { 

    if(rmdir($path."/".$file)) { 
     $debugStr .= "Deleted Directory: ".$file."<br />"; 
    } 

} 

編輯:rmdir只能處理空迪爾斯,你可能會如報告命令rmdir的頁面評論使用此解決方案:

function rrmdir($dir) { 
    foreach(glob($dir . '/*') as $file) { 
     if(is_dir($file)) 
      rrmdir($file); 
     else 
      unlink($file); 
    } 
    rmdir($dir); 
} 

它只是遞歸刪除一切$dir,然後擺脫目錄本身的。

+0

rmdir只能刪除空目錄。 – S3Mi

+0

@S3Mi我不好,我會提供一個遞歸解決方案。 – moonwave99

0

您可以使用系統命令ex。 exec("rm -rf {$dirPath}");或者如果你想通過PHP來做,你必須遞歸,循環不會做到這一點。

public function deleteDir($path) { 

    if ($handle = opendir($path)) { 

     while (false !== ($file = readdir($handle))) { 

      if ($file != "." && $file != "..") { 

       // IF IT"S A FILE THEN DELETE IT 
       if(is_file($path."/".$file)) { 

        if(unlink($path."/".$file)) { 
         $debugStr .= "Deleted File: ".$file."<br />"; 
        } 

       } else { 
        deleteDir($path."/".$file."/"); 
        rmdir($path."/".$file); 
       } 
      } 
     } 
    } 
} 

當我用下面的代碼,怎麼可能做到這一點?使用: 的/ dev /圖片/諾曼/ 8 - >不會刪除文件夾8使用: 的/ dev /圖片/諾曼/ - >將刪除所有文件夾

我覺得你的問題是,你錯過「/」,在結束 「的/ dev /圖片/諾曼/ 8」

0

我添加了一個$exclude參數去你的函數,這參數它是一個數組要從排除目錄的名字被刪除,就像這樣:

$path = $_SERVER['DOCUMENT_ROOT'].'/dev/images/norman/'; 

emptyDir($path); //will delete all under /norman/ 
emptyDir($path, array('8')); //will delete all under /norman/ except dir 8 
emptyDir($path, array('8','10')); //will delete all under /norman/ except dir 8 and 10 

function emptyDir($path,$exclude=false) { 

    // INITIALIZE THE DEBUG STRING 
    $debugStr = ''; 
    $debugStr .= "Deleting Contents Of: $path<br /><br />"; 
    if (!$exclude) { 
     $exclude = array(); 
    } 

    // PARSE THE FOLDER 
    if ($handle = opendir($path)) { 

     while (false !== ($file = readdir($handle))) { 

      if ($file != "." && $file != "..") { 

       // IF IT"S A FILE THEN DELETE IT 
       if(is_file($path."/".$file)) { 

        if(unlink($path."/".$file)) { 
        $debugStr .= "Deleted File: ".$file."<br />"; 
        } 

       } else if (!in_array($file, $exclude)) { 

        // IT IS A DIRECTORY 
        // CRAWL THROUGH THE DIRECTORY AND DELETE IT'S CONTENTS 

        if($handle2 = opendir($path."/".$file)) { 

         while (false !== ($file2 = readdir($handle2))) { 

          if ($file2 != "." && $file2 != "..") { 
           if(unlink($path."/".$file."/".$file2)) { 
           $debugStr .= "Deleted File: $file/$file2<br />";  
           } 
          } 

         } 

        } 

        if(rmdir($path."/".$file)) { 
        $debugStr .= "Directory: ".$file."<br />"; 
        } 

       } 

      } 

     } 

    } 
    echo $debugStr; 
} 
0
$path='./ggg'; 
rrmdir($path); 
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); 
} 
} 
相關問題