Home > Backend Development > PHP Problem > How to delete all folders in php

How to delete all folders in php

藏色散人
Release: 2023-03-09 22:54:02
Original
2744 people have browsed it

php method to delete all folders: first create a PHP sample file; then set the folders that need to be deleted; finally, traverse all files and folders in a folder and delete all folders and sub-files All files in the folder will do.

How to delete all folders in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php deletes all folders and files under a path Method

php traverses all files and folders in a folder, and deletes the code of all files under all folders and subfolders, achieving the effect of clearing a directory through recursion. This article will share the example code with you. Friends who need it can refer to it

php traverses all files and folders in a folder and deletes all files in all folders and subfolders. The code achieves the effect of clearing a directory through recursion. The code is simple and practical.

is also suitable for clearing the cache in thinkphp. In thinkphp, you can write the following code into the ./Application/Admin/Common/function.php file, and then call this function in the controller to perform the cleaning operation.

Functions used:

scandir($path)    遍历一个文件夹所有文件并返回数组。
unlink($filename)    删除文件。
rmdir($path)    只删除空文件夹
Copy after login
<?php
 //设置需要删除的文件夹
  $path = "./Application/Runtime/";
  //清空文件夹函数和清空文件夹后删除空文件夹函数的处理
  function deldir($path){
   //如果是目录则继续
   if(is_dir($path)){
    //扫描一个文件夹内的所有文件夹和文件并返回数组
   $p = scandir($path);
   foreach($p as $val){
    //排除目录中的.和..
    if($val !="." && $val !=".."){
     //如果是目录则递归子目录,继续操作
     if(is_dir($path.$val)){
      //子目录中操作删除文件夹和文件
      deldir($path.$val.&#39;/&#39;);
      //目录清空后删除空文件夹
      @rmdir($path.$val.&#39;/&#39;);
     }else{
      //如果是文件直接删除
      unlink($path.$val);
     }
    }
   }
  }
  }
 //调用函数,传入路径
 deldir($path);
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete all folders in php. For more information, please follow other related articles on the PHP Chinese website!

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