Home > Backend Development > PHP Tutorial > PHP deletion is not an empty directory implementation code_PHP tutorial

PHP deletion is not an empty directory implementation code_PHP tutorial

WBOY
Release: 2016-07-13 16:54:41
Original
945 people have browsed it

The built-in function rmdir in PHP can only delete empty directories. If you want to delete a directory with files or directories, you need to use it recursively with unlink. Let’s take a look at the custom function that deletes directories that are not empty.

php tutorial deleting non-empty directory implementation code
This tutorial first briefly introduces rmdir to delete empty directories, and then extends it to the writing and implementation code of custom functions that delete non-empty directories.
*/
//rmdir(dir,context) The rmdir() function deletes empty directories.

$path ='';
if( is_dir( $path ) )
{
if( rmdir( $path ) )
{
echo 'Delete directory successfully';
}
}
else
{
echo 'Not a directory';
}

/*
Summary
The function rmdir that comes with PHP can only delete empty directories. If you want to delete a directory with files or directories, you need to use it recursively with unlink. Let's take a look at the custom function that deletes directories that are not empty.
*/
/**
* Delete files or folders (recursively)
* @param array $filelist
* @param string $option
* @param string $fileext The file extension to be deleted Format: 'html'
* @return void
​*/

function rm($filelist, $option='r', $fileext = null, $if_rmdir = false) {
if (!is_array($filelist)) {
$filelist = explode('|', $filelist);
}
foreach ($filelist as $filename) {
If (is_file($filename)) {
If (empty($fileext)) {
​​​unlink($filename);
} else {
If (substr(strrchr($filename, '.'), 1 ) == $fileext){
​​​unlink($filename);
}
}
} elseif (is_dir($filename)) {
If (strpos($option, 'r')!==false) {
$file_list_ = ls($filename);
foreach ($file_list_ as $fi => $file) {
$file_list_[$fi] = $filename . $file;
}
​​ rm($file_list_, $option, $fileext);
}
If ($if_rmdir) {
​​ rmdir($filename);
}
}
}
}

//Call method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631728.htmlTechArticleThe built-in function rmdir in php can only delete empty directories. If you want to delete files or directories, Directories need to be used recursively with unlink. Let’s look at deleting directories that are not empty...
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