php遞迴刪除目錄及檔案的自訂函數rrmdir

來源:互聯網
上載者:User
  1. /**
  2. 遞迴刪除目錄及其下的所有檔案
  3. func: rrmdir
  4. */
  5. function rrmdir($dir) {
  6. if (is_dir($dir)) {
  7. $objects = scandir($dir);
  8. foreach ($objects as $object) {
  9. if ($object != “.” && $object != “..”) {
  10. if (filetype($dir.”/”.$object) == “dir”) rrmdir($dir.”/”.$object); else unlink($dir.”/”.$object);
  11. }
  12. }
  13. reset($objects);
  14. }
  15. }
  16. ?>
複製代碼

附:rmdir(PHP 4, PHP 5)rmdir — 刪除目錄Report a bug 說明bool rmdir ( string $dirname )嘗試刪除 dirname 所指定的目錄。 該目錄必須是空的,而且要有相應的許可權。成功時返回 TRUE, 或者在失敗時返回 FALSE.Note: 自 PHP 5.0.0 起 rmdir() 也可用於某些 URL 封裝協議。參見Supported Protocols and Wrappers 的列表看看 rmdir() 支援哪些 URL 封裝協議。Note: 在 PHP 5.0.0 中增加了 對上下文(Context)的支援。有關 上下文(Context) 的說明參見 Stream 函數。Note: 當啟用 安全模式時, PHP 會在執行指令碼時檢查被指令碼操作的目錄是否與被執行的指令碼有相同的 UID(所有者)。參見 mkdir() 和 unlink()。

  1. function rrmdir($dir) {
  2. if (is_dir($dir)) {
  3. $objects = scandir($dir);
  4. foreach ($objects as $object) {
  5. if ($object != "." && $object != "..") {
  6. if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
  7. }
  8. }
  9. reset($objects);
  10. rmdir($dir);
  11. }
  12. }
  13. ?>
複製代碼

This isn't my code, but just thought I would share, since it took me so long to find. This is a function to delete a folder, all sub-folders, and files in one clean move.Just tell it what directory you want deleted, in relation to the page that this function is executed. Then set $empty = true if you want the folder just emptied, but not deleted. If you set $empty = false, or just simply leave it out, the given directory will be deleted, as well.

  1. function deleteAll($directory, $empty = false) {
  2. if(substr($directory,-1) == "/") {
  3. $directory = substr($directory,0,-1);
  4. }
  5. if(!file_exists($directory) || !is_dir($directory)) {
  6. return false;
  7. } elseif(!is_readable($directory)) {
  8. return false;
  9. } else {
  10. $directoryHandle = opendir($directory);
  11. while ($contents = readdir($directoryHandle)) {
  12. if($contents != '.' && $contents != '..') {
  13. $path = $directory . "/" . $contents;
  14. if(is_dir($path)) {
  15. deleteAll($path);
  16. } else {
  17. unlink($path);
  18. }
  19. }
  20. }
  21. closedir($directoryHandle);
  22. if($empty == false) {
  23. if(!rmdir($directory)) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. }
  30. ?>
複製代碼

A patch to previous script to make sure rights for deletion is set:

  1. //Delete folder function
  2. function deleteDirectory($dir) {
  3. if (!file_exists($dir)) return true;
  4. if (!is_dir($dir) || is_link($dir)) return unlink($dir);
  5. foreach (scandir($dir) as $item) {
  6. if ($item == '.' || $item == '..') continue;
  7. if (!deleteDirectory($dir . "/" . $item)) {
  8. chmod($dir . "/" . $item, 0777);
  9. if (!deleteDirectory($dir . "/" . $item)) return false;
  10. };
  11. }
  12. return rmdir($dir);
  13. }
  14. ?>
複製代碼

更多內容,請參考 http://cn.php.net/rmdir 。

>>>

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.