本文介绍了PHP递归删除功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了递归PHP函数来删除文件夹。我想知道,如何修改此功能以删除webhosting中的所有文件和文件夹,不包括文件和文件夹名称的数组(例如cgi-bin,.htaccess)?BTW
使用此功能完全删除像这样的目录调用
recursive_remove_directory( '路径/到/目录/到/删除');
使用此函数清空一个目录调用,如下所示:
recursive_remove_directory('path / to / full_directory',TRUE);
现在函数是
函数recursive_remove_directory($ directory,$ empty = FALSE)
{
//如果路径最后一个斜杠我们在这里删除它
if(substr( $ directory,-1)=='/')
{
$ directory = substr($ directory,0,-1);
}
//如果路径无效或不是目录...
if(!file_exists($ directory)||!is_dir($ directory))
{
// ...我们返回false并退出函数
return FALSE;
//如果路径不可读
} elseif(!is_readable($ directory))
{
// ...我们返回false并退出函数
return FALSE;
//如果路径可读,则else else
} else {
//我们打开目录
$ handle = opendir($目录);
//并扫描
中的项目while(FALSE!==($ item = readdir($ handle)))
{
//如果filepointer不是当前目录
//或父目录
if($ item!='。&&$&$ item!='..')
{
//我们构建新路径以删除
$ path = $ directory。'/'。$ item;
//如果新路径是目录
if(is_dir($ path))
{
//我们用新路径$ b调用此函数$ b recursive_remove_directory($ path);
//如果新路径是文件
} else {
//我们删除文件
unlink($ path);
}
}
}
//关闭目录
closedir($ handle);
//如果空的选项未设置为true
if($ empty == FALSE)
{
//尝试删除现在的空目录
if(!rmdir($ directory))
{
//如果不可能返回false
return FALSE;
}
}
// return success
return TRUE;
}
}
解决方案
尝试这样做:
$ it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('%yourBaseDir%'),
RecursiveIteratorIterator :: CHILD_FIRST
);
$ excludeDirsNames = array();
$ excludeFileNames = array('。htaccess');
foreach($ it as $ entry){
if($ entry-> isDir()){
if(!in_array($ entry-> getBasename() ,$ excludeDirsNames)){
try {
rmdir($ entry-> getPathname());
}
catch(异常$ ex){
// dir not empty
}
}
}
elseif(!in_array($ entry-> getFileName(),$ excludeFileNames)){
unlink($ entry-> getPathname());
}
}
I wrote recursive PHP function for folder deletion. I wonder, how do I modify this function to delete all files and folders in webhosting, excluding given array of files and folder names (for ex. cgi-bin, .htaccess)?
BTW
to use this function to totally remove a directory calling like this
recursive_remove_directory('path/to/directory/to/delete');
to use this function to empty a directory calling like this:
recursive_remove_directory('path/to/full_directory',TRUE);
Now the function is
function recursive_remove_directory($directory, $empty=FALSE)
{
// if the path has a slash at the end we remove it here
if(substr($directory,-1) == '/')
{
$directory = substr($directory,0,-1);
}
// if the path is not valid or is not a directory ...
if(!file_exists($directory) || !is_dir($directory))
{
// ... we return false and exit the function
return FALSE;
// ... if the path is not readable
}elseif(!is_readable($directory))
{
// ... we return false and exit the function
return FALSE;
// ... else if the path is readable
}else{
// we open the directory
$handle = opendir($directory);
// and scan through the items inside
while (FALSE !== ($item = readdir($handle)))
{
// if the filepointer is not the current directory
// or the parent directory
if($item != '.' && $item != '..')
{
// we build the new path to delete
$path = $directory.'/'.$item;
// if the new path is a directory
if(is_dir($path))
{
// we call this function with the new path
recursive_remove_directory($path);
// if the new path is a file
}else{
// we remove the file
unlink($path);
}
}
}
// close the directory
closedir($handle);
// if the option to empty is not set to true
if($empty == FALSE)
{
// try to delete the now empty directory
if(!rmdir($directory))
{
// return false if not possible
return FALSE;
}
}
// return success
return TRUE;
}
}
解决方案
Try something like this:
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('%yourBaseDir%'),
RecursiveIteratorIterator::CHILD_FIRST
);
$excludeDirsNames = array();
$excludeFileNames = array('.htaccess');
foreach($it as $entry) {
if ($entry->isDir()) {
if (!in_array($entry->getBasename(), $excludeDirsNames)) {
try {
rmdir($entry->getPathname());
}
catch (Exception $ex) {
// dir not empty
}
}
}
elseif (!in_array($entry->getFileName(), $excludeFileNames)) {
unlink($entry->getPathname());
}
}
这篇关于PHP递归删除功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!