PHP目录树-删除文件夹


PHP Directory Tree - Deleting Folders

我发现了一个脚本,列出了所有目录和文件,并允许删除文件。

我有两个问题:

1)如何添加删除文件夹的功能(包含文件夹中的所有内容)2)我如何使脚本不允许用户浏览目录?(例如,用户的文件夹是./files/$userid/。我想让用户无法进入./files/./files/$userid/以外的文件夹进行任何更改

$script = basename(__FILE__); // the name of this script
                              $path   = !empty($_REQUEST['path']) ? $_REQUEST['path'] : dirname('./files/' . (string)$userid . '/'); // the path the script should access
                              $unlink = $_REQUEST['unlink'];

                              if(!empty($unlink)){
                                $unlink = realpath("$path/$unlink");
                                if(is_writable($unlink) && !unlink($unlink)){
                                  echo "<div class='"error'">Unable to delete file: $unlink</div>";
                                }
                              }
                              echo "<p>Browsing Location: {$path}</p>";
                              $directories = array();
                              $files       = array();
                              // Check we are focused on a dir
                              if (is_dir($path)){
                                chdir($path); // Focus on the dir
                                if ($handle = opendir('.')){
                                  while (($item = readdir($handle)) !== false) {
                                    // Loop through current directory and divide files and directorys
                                    if(is_dir($item)){
                                      array_push($directories, realpath($item));
                                    }
                                    else{
                                      array_push($files, ($item));
                                    }
                                  }
                                  closedir($handle); // Close the directory handle
                                }
                                else {
                                  echo "<p class='"error'">Directory handle could not be obtained.</p>";
                                }
                              }
                              else{
                                echo "<p class='"error'">Path is not a directory</p>";
                              }
                              // List the directories as browsable navigation
                              echo "<h2>Navigation</h2>";
                              echo "<ul>";
                              foreach($directories as $directory){
                                $delete = is_writable($file) ? "<a class='"unlink'" href='"{$script}?path={$path}&unlink={$directory}'">delete</a>" : '';
                                echo ($directory != $path) ? "<li><a href='"{$script}?path={$directory}'">{$directory}</a></li>" : "";
                              }
                              echo "</ul>";
                              echo "<h2>Files</h2>";
                              echo "<ul>";
                              foreach($files as $file){
                                // Comment the next line out if you wish see hidden files while browsing
                                if(preg_match("/^'./", $file) || $file == $script){continue;} // This line will hide all invisible files.
                                $delete = is_writable($file) ? "<a class='"unlink'" href='"{$script}?path={$path}&unlink={$file}'">delete</a>" : '';
                                echo '<li><a href="' . basename($file) . '" target="_blank">' . $file . "</a> $delete</li>";
                              }
                              echo "</ul>";

1)尝试使用此函数递归地删除文件夹(参见手册,用户贡献的注释部分http://php.net/manual/en/function.rmdir.php):

  function delTree($dir) { 
   $files = array_diff(scandir($dir), array('.','..')); 
    foreach ($files as $file) { 
      (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file"); 
    } 
    return rmdir($dir); 
  } 

2)最好开发一些文件管理器来允许用户浏览某些文件夹。或者查看一些现成的解决方案:http://www.jquery4u.com/plugins/10-jquery-file-manager-plugins/