0

I have found a script that lists all directories and files in them, and allows for the deletion of files.

I have two problems:

1) How do I add the function to delete a folder (With all the contents in it) 2) How do I make the script not to allow a user to browse up a directory? (For e.g. a user's folder is ./files/$userid/. I want it so that a user won't be able to go to or make any changes to ./files/ or a folder other then ./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
  • 1
    You can find multiple recursive folder delete scripts under rmdir in PHP manual Commented Jan 10, 2013 at 17:14

1 Answer 1

1

1) Try use this function to delete folder recursively (see manual, User contributed notes section 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) Better develop some filemanager to allow users browse certain folders. Or see some ready solutions: http://www.jquery4u.com/plugins/10-jquery-file-manager-plugins/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.