如何用PHP删除文件夹?

发布于 2024-08-16 17:31:00 字数 1208 浏览 8 评论 0原文

我可以在创建类别时创建一个图像文件夹,以便我可以在那里上传图像。

现在我想在删除类别时删除该文件夹。

创建文件夹的代码如下并且运行良好。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑疤怪 2024-08-23 17:31:00
$this->_remove_path($catname); // because previous parts you're using $catname

然后删除路径函数

// recursively remove all files and sub-folder in that particular folder
function _remove_path($folder){
    $files = glob( $folder . DIRECTORY_SEPARATOR . '*');
    foreach( $files as $file ){
        if($file == '.' || $file == '..'){continue;}
        if(is_dir($file)){
            $this->_remove_path( $file );
        }else{
            unlink( $file );
        }
    }
    rmdir( $folder ); 
}
$this->_remove_path($catname); // because previous parts you're using $catname

Then the remove path function

// recursively remove all files and sub-folder in that particular folder
function _remove_path($folder){
    $files = glob( $folder . DIRECTORY_SEPARATOR . '*');
    foreach( $files as $file ){
        if($file == '.' || $file == '..'){continue;}
        if(is_dir($file)){
            $this->_remove_path( $file );
        }else{
            unlink( $file );
        }
    }
    rmdir( $folder ); 
}
素食主义者 2024-08-23 17:31:00

您需要使用 unlinkrmdir:

$handler = opendir($folder);
if (!$handler) {
    trigger_error('File Error: Failed to open the directory ' . $folder, E_USER_ERROR);
    return false;
}

// list the files in the directory
while ($file = readdir($handler)) {
    // if $file isn't this directory or its parent,
    if ($file != '.' && $file != '..' && !is_dir($file)) {
        // delete it
        if (!unlink($file)) {
            trigger_error('File Error: Failed to remove file ' . $file, E_USER_ERROR);
        }
    }
}

// tidy up: close the handler
closedir($handler);

if (!rmdir($folder)) {
    trigger_error('File Error: Failed to remove folder ' . $folder, E_USER_ERROR);
}

You'll need to used unlink and rmdir:

$handler = opendir($folder);
if (!$handler) {
    trigger_error('File Error: Failed to open the directory ' . $folder, E_USER_ERROR);
    return false;
}

// list the files in the directory
while ($file = readdir($handler)) {
    // if $file isn't this directory or its parent,
    if ($file != '.' && $file != '..' && !is_dir($file)) {
        // delete it
        if (!unlink($file)) {
            trigger_error('File Error: Failed to remove file ' . $file, E_USER_ERROR);
        }
    }
}

// tidy up: close the handler
closedir($handler);

if (!rmdir($folder)) {
    trigger_error('File Error: Failed to remove folder ' . $folder, E_USER_ERROR);
}
棒棒糖 2024-08-23 17:31:00

我对 Darryl Hein代码对我来说就像一个魅力。

function remove_path2($path) {
    if(is_dir($path)) {
        $handler = opendir($path);
        if (!$handler) {
            trigger_error('File Error: Failed to open the directory ' . $path, E_USER_ERROR);
            return;
        }

        // list the files in the directory
        while ($file = readdir($handler)) {
            if ($file != '.' && $file != '..')
                remove_path2($path.DIRECTORY_SEPARATOR.$file);
        }

        // tidy up: close the handler
        closedir($handler);

        if (!rmdir($path)) {
            trigger_error('File Error: Failed to remove folder ' . $path, E_USER_ERROR);
        }
    }
    else {
        // delete it
        if (!unlink($path)) {
            trigger_error('File Error: Failed to remove file ' . $path, E_USER_ERROR);
        }
    }
}

My modified version of Darryl Hein's Code works like a charm to me.

function remove_path2($path) {
    if(is_dir($path)) {
        $handler = opendir($path);
        if (!$handler) {
            trigger_error('File Error: Failed to open the directory ' . $path, E_USER_ERROR);
            return;
        }

        // list the files in the directory
        while ($file = readdir($handler)) {
            if ($file != '.' && $file != '..')
                remove_path2($path.DIRECTORY_SEPARATOR.$file);
        }

        // tidy up: close the handler
        closedir($handler);

        if (!rmdir($path)) {
            trigger_error('File Error: Failed to remove folder ' . $path, E_USER_ERROR);
        }
    }
    else {
        // delete it
        if (!unlink($path)) {
            trigger_error('File Error: Failed to remove file ' . $path, E_USER_ERROR);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文