Home  >  Article  >  Backend Development  >  Delete files and corresponding folders

Delete files and corresponding folders

WBOY
WBOYOriginal
2016-07-25 09:11:31857browse
Delete directory and contained files function
  1. //Note $dir file name eg: admin/runtime
  2. //Delete directory and contained file function
  3. function deldir($dir) {
  4. //Open file directory
  5. $dh = opendir ($dir);
  6. //Loop to read files
  7. while ($file = readdir($dh)) {
  8. if($file != '.' && $file != '..') {
  9. $fullpath = $dir . '/' . $file;
  10. //Determine whether it is a directory
  11. if(!is_dir($fullpath)) {
  12. echo $fullpath."Deleted
    ";
  13. //If not, Delete the file
  14. if(!unlink($fullpath)) {
  15. }
  16. } else {
  17. //If it is a directory, delete the lower-level directory recursively
  18. deldir($fullpath);
  19. }
  20. }
  21. }
  22. //Close the directory
  23. closedir($dh);
  24. //Delete directory
  25. //if(rmdir($dir)) {
  26. // return true;
  27. // } else {
  28. // return false;
  29. // }
  30. }
  31. ?>
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