【问题标题】:How to remove folder with PHP?如何使用 PHP 删除文件夹?
【发布时间】:2010-01-01 09:40:47
【问题描述】:

我可以在创建分类的时候创建一个图片文件夹,这样我就可以在那里上传图片了。

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

创建文件夹的代码如下,效果很好。

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 以便它不会删除图像文件夹。我想删除 images 文件夹的唯一子文件夹。

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)
{

}

我不知道在此之后如何进行。

谁能给我一些建议?

【问题讨论】:

标签: php rmdir


【解决方案1】:
$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 ); 
}

【讨论】:

  • 你能解释一下foreach发生了什么吗?请。
  • 它会在删除之前循环查看该子文件夹中是否有目录或文件。然后它将删除子文件夹或文件。如果您要删除的文件夹中仍有文件或子文件夹,rmdir 将失败。
【解决方案2】:

您需要使用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);
}

【讨论】:

    【解决方案3】:

    Darryl HeinCode 的修改版本对我来说就像是一种魅力。

    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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 2010-12-22
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2016-03-29
      • 2016-10-24
      • 2011-07-24
      • 2011-11-24
      相关资源
      最近更新 更多