如何用PHP编写递归函数彻底删除指定目录?

更新于
2026-09-27 14:15:45
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计70个文字,预计阅读时间需要1分钟。

如何用PHP编写递归函数彻底删除指定目录?

phpfunction delete_directory($dir) { if ($dh=opendir($dir)) { while (($file=readdir($dh)) !==false) { if ($file==. || $file==..) continue; if (is_dir($dir . '/' . $file)) delete_directory($dir . '/' . $file); else unlink($file); } closedir($dh); }}

如何用PHP编写递归函数彻底删除指定目录?

<?php function delete_directory($dir) { if ($dh = @opendir($dir)) { while (($file = readdir ($dh)) != false) { if (($file == ".") || ($file == "..")) continue; if (is_dir($dir . '/' . $file)) delete_directory($dir . '/' . $file); else unlink($dir . '/' . $file); } @closedir($dh); rmdir($dir); } } $dir = "./fakeDir"; delete_directory($dir); ?>

本文共计70个文字,预计阅读时间需要1分钟。

如何用PHP编写递归函数彻底删除指定目录?

phpfunction delete_directory($dir) { if ($dh=opendir($dir)) { while (($file=readdir($dh)) !==false) { if ($file==. || $file==..) continue; if (is_dir($dir . '/' . $file)) delete_directory($dir . '/' . $file); else unlink($file); } closedir($dh); }}

如何用PHP编写递归函数彻底删除指定目录?

<?php function delete_directory($dir) { if ($dh = @opendir($dir)) { while (($file = readdir ($dh)) != false) { if (($file == ".") || ($file == "..")) continue; if (is_dir($dir . '/' . $file)) delete_directory($dir . '/' . $file); else unlink($dir . '/' . $file); } @closedir($dh); rmdir($dir); } } $dir = "./fakeDir"; delete_directory($dir); ?>