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

How to delete specified files and folders in Php

WBOY
Release: 2016-07-25 08:55:57
Original
1150 people have browsed it
  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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template