如何使用PHP删除文件夹?

问题描述:

我可以在创建类别时创建图像文件夹,以便我可以在那里上传图像。如何使用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) 
{ 

} 

我不确定如何在此之后继续。

任何人都可以给我一些建议吗?

+0

杜佩:http://*.com/questions/1447791/delete-directory-in-php – 2010-01-01 09:55:13

+0

这里太:http://*.com/questions/1334398/how-to-delete-a -folder-with-contents-using-php – 2010-01-01 09:56:43

$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); 
} 
+0

你能解释一下foreach中发生了什么吗?请。 – shin 2010-01-01 09:53:53

+0

它会在删除前循环查看该子文件夹中是否有目录或文件。它会删除子文件夹或文件。如果您要删除的文件夹中仍有文件或子文件夹,rmdir将会失败。 – mauris 2010-01-01 09:55:29

+0

它的工作原理,谢谢。 – shin 2010-01-01 10:04:44

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

我修改的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); 
     } 
    } 
}