Dodona gives you answers

How can I remove a directory from my webserver?

leave a comment »

The most obvious way would be to use an FTP client. But sometimes I run into the problem that directories, created with for example a CMS system that runs on the server, have another owner (namely the script that created them). That way it may happen that my FTP client doesn’t have enough rights to remove the directory. If this happens after I deleted the server-side script that created the directory, I am stuck with a directory I can’t get rid of.

This is where the PHP code below comes in handy. It removes a directory and its contents from the server. Because in situations as described above it usually has the same rights as the script that created te directory, it is likely to succeed where your FTP client might fail.

NOTE: be careful not to leave the script on your server after you have used it for obvious reasons…

function removeDir($path) {
   // Add trailing slash to $path if one is not there
   if (substr($path, -1, 1) != "/") {
      $path .= "/";
   $normal_files = glob($path . "*");
   $hidden_files = glob($path . "\.?*");
   $all_files = array_merge($normal_files, $hidden_files);
   foreach ($all_files as $file) {
      // Skip pseudo links to current and parent dirs (./ and ../).
      if (preg_match("/(\.|\.\.)$/", $file)) continue;
      if (is_file($file) === TRUE) {
         // Remove each file in this Directory
         echo "Removed File: " . $file . "<br>";
      } else if (is_dir($file) === TRUE) {
         // If this Directory contains a Subdirectory, run this Function on it
   // Remove Directory once Files have been removed (If Exists)
   if (is_dir($path) === TRUE) {
      echo "<br>Removed Directory: " . $path . "<br><br>";
// To remove a dir:

Written by lutsen

August 26, 2008 at 1:44 pm

Posted in PHP, Web

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: