$valuecan =语言文件的文件夹结构.示例:languages/english.php
$value也可以=文件名.示例:english.php
因此,如果该目录$value中没有其他文件/文件夹(删除实际文件,因为我已经在,当然),我需要获取当前文件夹并删除该文件夹.
foreach($module['languages'] as $lang => $langFile)
{
foreach ($langFile as $type => $value)
{
@unlink($module_path . '/' . $value);
// Now I need to delete the folder ONLY if there are no other directories inside the folder where it is currently at.
// And ONLY if there are NO OTHER files within that folder also.
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点??并且想知道这是否可以在不使用while循环的情况下完成,因为while循环中的foreach循环可能需要一些时间,并且需要尽可能快.
仅仅是FYI,永远不应该删除$ module_path.所以,如果$value = english.php,它永远不应该删除$ module_path.当然,那里总会有另一个文件,所以检查这个是没有必要的,但不会受到任何影响.
多谢你们 :)
编辑
好的,现在我在这里使用这个代码,它不工作,它不是删除文件夹或文件,我也没有得到任何错误...所以不知道这里的问题是什么:
foreach($module['languages'] as $lang => $langFile)
{
foreach ($langFile as $type => $value)
{
if (@unlink($module_path . '/' . $value))
@rmdir(dirname($module_path . '/' . $value));
}
}
Run Code Online (Sandbox Code Playgroud)
NEVERMIND,这是一个CHARM !!! 欢呼大家!!
最简单的方法是尝试使用rmdir.如果文件夹不为空,则不删除该文件夹
rmdir($module_path);
Run Code Online (Sandbox Code Playgroud)
你也可以检查文件夹是空的
if(count(glob($module_path.'*'))<3)//delete
Run Code Online (Sandbox Code Playgroud)
2为.和..
UPD:正如我所评论的那样,你应该用dirname替换$ module_path($ module_path.'.'.$ value);