PHP unlink and rmdir delete directory and all file instance codes in the directory
- 2021-09-11 19:51:21
- OfStack
Deleting files and directories in php is actually very simple as long as two functions are unlink1 and rmdir functions. If we want to delete directories and files under directories, we need to use recursion to operate.
Function code: Only delete the files under the specified directory, not delete the directory folder, the code is as follows:
class shanchu {
// All files in the circular directory
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 {// Open source code phpfensi.com
if( unlink( " $dirName/$item " ) )echo "File deleted successfully: $dirName/$item<br />n " ;
}
}
}
closedir( $handle );
}
}
}
Suppose you need to delete all files in a directory named "upload", but you don't need to delete the directory folder. You can do this by the following code:
<?php delFileUnderDir( ' upload');?>
php Delete all directories with the following code:
function deltree($pathdir)
{
echo $pathdir;// Used when debugging
if(is_empty_dir($pathdir))// If it is empty
{
rmdir($pathdir);// Direct deletion
}
else
{// Otherwise, read this directory, except . And .. Outside
$d=dir($pathdir);
while($a=$d->read())
{
if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..')){unlink($pathdir.'/'.$a);}
// If it is a file, delete it directly
if(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..'))
{// If it is a directory
if(!is_empty_dir($pathdir.'/'.$a))// Whether it is empty or not
{// If not, call itself, but the original path + The directory name of his subordinate
deltree($pathdir.'/'.$a);
}
if(is_empty_dir($pathdir.'/'.$a))
{// If it is empty, delete it directly
rmdir($pathdir.'/'.$a);
}
}
}
$d->close();
echo " You must first delete all files in the directory ";// I used it when debugging
}
}
function is_empty_dir($pathdir)
{
// Determine whether the directory is empty
$d=opendir($pathdir);
$i=0;
while($a=readdir($d))
{
$i++;
}
closedir($d);
if($i>2){return false;}
else return true;
}
PHP delete the directory and all files under the directory, the code is as follows:
<?php
// Loop to delete directories and files function
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 "File deleted successfully: $dirName/$item<br />n " ;
}
}
}
closedir( $handle );
if( rmdir( $dirName ) )echo "Directory deleted successfully: $dirName<br />n " ;
}
}
// Assuming you need to delete 1 His name is " upload The sibling directory of "is all the files in this directory, which you can do by the following code:
delDirAndFile( ' upload');
?>
Summarize