Heim  >  Artikel  >  Backend-Entwicklung  >  Php删除指定文件与文件夹的方法

Php删除指定文件与文件夹的方法

WBOY
WBOYOriginal
2016-07-25 08:55:571080Durchsuche
  1. //删除指定目录(文件夹)中的所有文件函数
  2. function delfile($dir) {
  3. if (is_dir($dir)) {
  4. $dh=opendir($dir);//打开目录
  5. //列出目录中的所有文件并去掉 . 和 ..
  6. while (false !== ( $file = readdir ($dh))) {
  7. if($file!="." && $file!="..") {
  8. $fullpath=$dir."/".$file;
  9. if(!is_dir($fullpath)) {
  10. unlink($fullpath);//删除目录中的所有文件
  11. } else {
  12. delfile($fullpath);
  13. }
  14. }
  15. closedir($dh);
  16. }
  17. }
  18. }
  19. //删除指定的目录
  20. function deldir($dir) {
  21. delfile($dir);
  22. if (is_dir($dir)) {
  23. rmdir($dir); //目录必须是空的
  24. }
  25. }
  26. ?>
复制代码

猜你喜欢: 删除指定文件夹中所有文件的php代码 调用示例: 1,删除D盘中的“myphoto”文件夹中的所有文件

  1. $dir="D:/myphoto";
  2. delfile($dir);
  3. ?>
复制代码

2,删除D盘中的“myphoto”文件夹

  1. $dir="D:/myphoto";
  2. deldir($dir);
  3. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn