Home > Backend Development > PHP Tutorial > PHP implements method of deleting file directories based on recursion

PHP implements method of deleting file directories based on recursion

墨辰丷
Release: 2023-03-28 21:16:01
Original
1579 people have browsed it

This article mainly introduces in detail how PHP uses recursion to delete file directories. It has certain reference value. Interested friends can refer to

to delete the directory directly. If it is empty Directories can be deleted. If the directory is not empty, you can only delete the files in the directory first and then delete the directory. I encapsulated a delete function and then called this function directly. You can use it directly if you like it. The encoding is gbk. Please pay attention to the encoding when using it.

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/10 0010
 * Time: 20:27
 */
//删除文件,先删除文件内部的文件,再删除文件夹
header("Content-type: text/html; charset=gbk");
function deldir($dir){
 $dh=opendir($dir);
 while($file=readdir($dh)){
 if($file!="." && $file!=".."){//判断是不是本目录和上级目录
 if(!is_dir($dir."/".$file)){
  unlink($dir."/".$file);
 }else{
  //递归
  deldir($dir."/".$file);
 }
 }
 }
 closedir($dh);
 if(rmdir($dir)){
 return true;
 }else{
 return false;
 }
 }
 //删除函数结束
 if(deldir("test")){
 echo &#39;删除文件成功&#39;;
 die();
 }else
 echo &#39;删除文件失败&#39;;
 die();
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php Detailed explanation of generating signatures and verifying signatures

PHP in POST mode Submit XML, obtain XML, parse XML detailed explanation and examples_phpreal

##php Detailed explanation of the four methods of parsing xml

The above is the detailed content of PHP implements method of deleting file directories based on recursion. For more information, please follow other related articles on the PHP Chinese website!

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