Home > Backend Development > PHP Tutorial > How to delete files in a directory using PHP unlink and rmdir

How to delete files in a directory using PHP unlink and rmdir

php中世界最好的语言
Release: 2023-03-26 19:10:01
Original
1832 people have browsed it

This time I will show you how to delete files in the directory with PHP unlink and rmdir. What are the precautions for PHP unlink and rmdir to delete files in the directory. The following is a practical case, let's take a look.

Function code: Only delete files in the specified directory, without deleting directory folders. The code is as follows:

class shanchu { 
//循环目录下的所有文件 
function delFileUnderDir( $dirName=”../Smarty/templates/templates_c” ) 
{ 
if ( $handle = opendir( “$dirName” ) ) { 
while ( false !== ( $item = readdir( $handle ) ) ) { 
if ( $item != “.” && $item != “..” ) { 
if ( is_dir( “$dirName/$item” ) ) { 
delFileUnderDir( “$dirName/$item” ); 
} else {//开源代码phpfensi.com 
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item<br />n”; 
} 
} 
} 
closedir( $handle ); 
} 
} 
}
Copy after login

Assume that all files in a directory named "upload" need to be deleted, but no To delete a directory folder, you can complete it with the following code:

<?php delFileUnderDir( ‘upload&#39;);?>
Copy after login

php deletes all directories, the code is as follows:

function deltree($pathdir) 
{ 
echo $pathdir;//调试时用的 
if(is_empty_dir($pathdir))//如果是空的 
{ 
rmdir($pathdir);//直接删除 
} 
else 
{//否则读这个目录,除了.和..外 
$d=dir($pathdir); 
while($a=$d->read()) 
{ 
if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..')){unlink($pathdir.'/'.$a);} 
//如果是文件就直接删除 
if(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..')) 
{//如果是目录 
if(!is_empty_dir($pathdir.'/'.$a))//是否为空 
{//如果不是,调用自身,不过是原来的路径+他下级的目录名 
deltree($pathdir.'/'.$a); 
} 
if(is_empty_dir($pathdir.'/'.$a)) 
{//如果是空就直接删除 
rmdir($pathdir.'/'.$a); 
} 
} 
} 
$d->close(); 
echo "必须先删除目录下的所有文件";//我调试时用的 
} 
} 
function is_empty_dir($pathdir) 
{ 
//判断目录是否为空 
$d=opendir($pathdir); 
$i=0; 
while($a=readdir($d)) 
{ 
$i++; 
} 
closedir($d); 
if($i>2){return false;} 
else return true; 
}
Copy after login

PHP deletes the directory and all files in the directory, the code is as follows:

<?php 
//循环删除目录和文件函数 
function delDirAndFile( $dirName ) 
{ 
if ( $handle = opendir( “$dirName” ) ) { 
while ( false !== ( $item = readdir( $handle ) ) ) { 
if ( $item != “.” && $item != “..” ) { 
if ( is_dir( “$dirName/$item” ) ) { 
delDirAndFile( “$dirName/$item” ); 
} else { 
if( unlink( “$dirName/$item” ) )echo “成功删除文件: $dirName/$item<br />n”; 
} 
} 
} 
closedir( $handle ); 
if( rmdir( $dirName ) )echo “成功删除目录: $dirName<br />n”; 
} 
} 
//假设需要删除一个名叫”upload”的同级目录即此目录下的所有文件,你可以通过以下代码完成: 
delDirAndFile( ‘upload'); 
?>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to use the redis counter class

Detailed explanation of the PHP half search algorithm case

The above is the detailed content of How to delete files in a directory using PHP unlink and rmdir. 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