Home >Backend Development >PHP Tutorial >How to delete specified files and folders in Php

How to delete specified files and folders in Php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:55:571169browse
  1. //Delete all files in the specified directory (folder) function
  2. function delfile($dir) {
  3. if (is_dir($dir)) {
  4. $dh=opendir($dir );//Open the directory
  5. //List all files in the directory and remove. and..
  6. while (false !== ( $file = readdir ($dh))) {
  7. if($file!=" ." && $file!="..") {
  8. $fullpath=$dir."/".$file;
  9. if(!is_dir($fullpath)) {
  10. unlink($fullpath);//Delete the directory All files
  11. } else {
  12. delfile($fullpath);
  13. }
  14. }
  15. closedir($dh);
  16. }
  17. }
  18. }
  19. //Delete the specified directory
  20. function deldir($dir) {
  21. delfile($ dir);
  22. if (is_dir($dir)) {
  23. rmdir($dir); //The directory must be empty
  24. }
  25. }
  26. ?>
Copy code

Guess you like: Delete the php code of all files in the specified folder Call example: 1. Delete all files in the "myphoto" folder in the D drive

  1. $dir="D:/myphoto";
  2. delfile($dir);
  3. ?>
Copy the code

2, delete "myphoto" in the D drive folder

  1. $dir="D:/myphoto";
  2. deldir($dir);
  3. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn