Home > Backend Development > PHP Tutorial > Example of recursively deleting directories in php

Example of recursively deleting directories in php

WBOY
Release: 2016-07-25 08:51:31
Original
1020 people have browsed it
  1. function deletedir($dir){
  2. if(!handle=@opendir($dir)){ //Detect whether the directory to be opened exists
  3. die("There is no such directory");
  4. }
  5. while(false !==($file=readdir($handle))){
  6. if($file!=="."&&$file!==".."){ //Exclude the current directory and parent level directory
  7. $file=$dir .DIRECTORY_SEPARATOR. $file;
  8. if(is_dir($file)){
  9. deletedir($file);
  10. }else{
  11. if(@unlink($file)){
  12. echo "file $fileDeletion successful.
    ";
  13. }else{
  14. echo "File$fileDeletion failed!
    ";
  15. }
  16. }
  17. }
  18. if(@rmdir($dir)){
  19. echo "Directory$dir was deleted successfully.
    n";
  20. }else{
  21. echo "Directory $dirDeletion failed!
    n";
  22. }
  23. }
  24. //Test program
  25. $dir="/var/www/test";
  26. deletedir($dir);
  27. ?>
Copy the code

Create some folders and files under the /var/www/test folder. shell> touch aaa shell> touch bbb shell> touch ccc shell> touch eee shell> touch ffff shell> mkdir 111 shell> mkdir 222 shell> mkdir 333 Create some files in the 111, 222, and 333 folders respectively, and then grant permissions. shell>chown www.www test -R

Then run del_files.php to detect the effect of recursively deleting the directory.

>>>> Articles you may be interested in: php code to delete records and delete image files at the same time php delete uploaded pictures and folders (example sharing) Simple sample code for uploading and deleting images in PHP Small example of PHP deleting all files created N minutes ago Example of how to delete a directory and all files in php php ftp class (copy, move, delete files, create directories, etc.) php implementation code for deleting records and refreshing the current page Delete the php code of all files in the specified folder Simple example of uploading and deleting images in php A function written in php to delete a directory php code for recursively creating and deleting folders php custom function rrmdir to recursively delete directories and files



Related labels:
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