Home  >  Article  >  Backend Development  >  How to use unlink to delete a folder in php

How to use unlink to delete a folder in php

王林
王林Original
2021-09-26 17:52:082252browse

How to use unlink to delete a folder in php: 1. Create a php sample file; 2. Pass "if ($handle = opendir( "$dirName" ) ) {while (false !== ($ item = readdir($handle))){if($item...}" statement can be used to delete the folder.

How to use unlink to delete a folder in php

The operating environment of this article: windows10 System, php 7, thinkpad t480 computer.

Deleting files in PHP is actually a very simple thing, because PHP has prepared appropriate functions for us, the unlink and rmdir functions. By using these two The function can also complete recursive deletion operations.

Let’s take a look at the specific implementation code!

The implementation 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 ); 
} 
} 
}

Assume that a name needs to be deleted Call all files in the "upload" directory, but there is no need to delete the directory folder. You can do it with the following code:

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

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.&#39;/&#39;.$a) && ($a!=&#39;.&#39;) && ($a!=&#39;..&#39;)){unlink($pathdir.&#39;/&#39;.$a);} 
//如果是文件就直接删除 
if(is_dir($pathdir.&#39;/&#39;.$a) && ($a!=&#39;.&#39;) && ($a!=&#39;..&#39;)) 
{//如果是目录 
if(!is_empty_dir($pathdir.&#39;/&#39;.$a))//是否为空 
{//如果不是,调用自身,不过是原来的路径+他下级的目录名 
deltree($pathdir.&#39;/&#39;.$a); 
} 
if(is_empty_dir($pathdir.&#39;/&#39;.$a)) 
{//如果是空就直接删除 
rmdir($pathdir.&#39;/&#39;.$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; 
}

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&#39;); 
?>

Recommended learning: php training

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

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