如何用PHP删除文件夹?
我可以在创建类别时创建一个图像文件夹,以便我可以在那里上传图像。
现在我想在删除类别时删除该文件夹。
创建文件夹的代码如下并且运行良好。
function create(){
if ($this->input->post('name')){
$this->MCats->addCategory();
$folder = $this->input->post('name');
$folder = strtolower($folder);
$folder = str_replace(" ", "_", $folder);
$folder = 'images/'.$folder;
$this->_create_path($folder);
...
...
}
function _create_path($folder)
{
// create dir if not exists
$folder = explode( "/" , $folder );
$mkfolder = "";
//sets the complete directory path
for( $i=0 ; isset( $folder[$i] ) ; $i++ )
{
$mkfolder .= $folder[$i] . '/';
if(!is_dir($mkfolder )) mkdir("$mkfolder");
}
}
我想出了以下代码。但我不知道如何使用 rmdir 这样它就不会删除图像文件夹。我想删除图像文件夹的唯一子文件夹。
function delete($id){
$cat = $this->MCats->getCategory($id);
// This will pull the name of category name.
$catname = $cat['name'];
$catname = strtolower($catname);
$catname = str_replace(" ", "_", $catname);
$catname = 'images/'.$catname;
$this->_remove_path($catname);
...
...
}
function _remove_path($folder)
{
}
我不知道此后如何继续。
有人可以给我一些建议吗?
I can create a image folder when I create a category, so that I can upload images there.
Now I want to delete that folder when I delete the category.
Code for creating a folder is the following and works well.
function create(){
if ($this->input->post('name')){
$this->MCats->addCategory();
$folder = $this->input->post('name');
$folder = strtolower($folder);
$folder = str_replace(" ", "_", $folder);
$folder = 'images/'.$folder;
$this->_create_path($folder);
...
...
}
function _create_path($folder)
{
// create dir if not exists
$folder = explode( "/" , $folder );
$mkfolder = "";
//sets the complete directory path
for( $i=0 ; isset( $folder[$i] ) ; $i++ )
{
$mkfolder .= $folder[$i] . '/';
if(!is_dir($mkfolder )) mkdir("$mkfolder");
}
}
And I come up with the following code. But I am not sure how to use rmdir so that it will not remove images folder. I want to remove only child of images folder.
function delete($id){
$cat = $this->MCats->getCategory($id);
// This will pull the name of category name.
$catname = $cat['name'];
$catname = strtolower($catname);
$catname = str_replace(" ", "_", $catname);
$catname = 'images/'.$catname;
$this->_remove_path($catname);
...
...
}
function _remove_path($folder)
{
}
I am not sure how to proceed after this.
Can anyone give me some suggestions please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然后删除路径函数
Then the remove path function
您需要使用 unlink 和 rmdir:
You'll need to used unlink and rmdir:
我对 Darryl Hein 的 代码对我来说就像一个魅力。
My modified version of Darryl Hein's Code works like a charm to me.