Joomla – unable deleting files through ftp

Uncategorized

Joomla creates files that cannot be deleted through FTP. If your host does not provide shell-access, you can only delete  these files through a php script. The joomlaXplorer-extension can delete most of those files, but not the ones owned by 'apache' I think. The script below is able to delete files and directories which are owned by 'apache' for sure. Be carefull with it!

<html>

<body> <?php
// ———— lixlpixel recursive PHP functions ————-
 // recursive_remove_directory( directory to delete, empty )
 // expects path to directory and optional TRUE / FALSE to empty
 // of course PHP has to have the rights to delete the directory
 // you specify and all files and folders inside the directory
// ————————————————————

// to use this function to totally remove a directory, write:
 // recursive_remove_directory('path/to/directory/to/delete');

 // to use this function to empty a directory, write:
 // recursive_remove_directory('path/to/full_directory',TRUE);

 function recursive_remove_directory($directory, $empty=FALSE)
 {
     // if the path has a slash at the end we remove it here
    if(substr($directory,-1) == '/')
     {
         $directory = substr($directory,0,-1);
    }
 
     // if the path is not valid or is not a directory …
     if(!file_exists($directory) || !is_dir($directory))
    {
         // … we return false and exit the function
         return FALSE;
 
     // … if the path is not readable
     }elseif(!is_readable($directory))
     {
         // … we return false and exit the function
         return FALSE;
 
     // … else if the path is readable
     }else{
 
         // we open the directory
         $handle = opendir($directory);
 
         // and scan through the items inside
         while (FALSE !== ($item = readdir($handle)))
         {
             // if the filepointer is not the current directory
             // or the parent directory
             if($item != '.' && $item != '..')
             {
                 // we build the new path to delete
                 $path = $directory.'/'.$item;
 
                 // if the new path is a directory
                 if(is_dir($path))
                 {
                     // we call this function with the new path
                     recursive_remove_directory($path);
 
                 // if the new path is a file
                 }else{
                     // we remove the file
                     unlink($path);
                 }
             }
         }
         // close the directory
         closedir($handle);
 
         // if the option to empty is not set to true
         if($empty == FALSE)
         {
             // try to delete the now empty directory
             if(!rmdir($directory))
             {
                 // return false if not possible
                 return FALSE;
             }
         }
         // return success
         return TRUE;
     }
 }
 // ————————————————————

if ($_POST['file'] ||  $_POST['dir'] ||  $_POST['nedir']) {

$myFile = $_POST['file'];
$myDir = $_POST['dir'];
$myNedir = $_POST['nedir'];

// delete file
if (file_exists($myFile)) {
  unlink($myFile);
  print("File removed.\n");
} else {
  print("File does not exist.\n");
}

// delete dir
if (file_exists($myDir)) {
  rmdir($myDir);
  print("Empty directory removed.\n");
} else {
  print("Empty directory does not exist.\n");
}

// delete dir
if (file_exists($myNedir)) {
  recursive_remove_directory($myNedir);
  print("Directory removed.\n");
} else {
  print("Directory does not exist.\n");
}

} else {

print<<<_HTML_

Delete files and folders that are owned by APACHE and thus cannot be deleted by any FTP program (script written by Robert Buzink).<br />
<form method="post" action='$_SERVER[PHP_SELF]'>
File: <input type="text" name="file">
Dir: <input type="text" name="dir">
Non-emty dir <input type="text" name="nedir">
<input type="submit" value="delete">
</form>

_HTML_;
}
?>

Tags: ,

2 Comments

  1. Sandor van Os says:

    dank, dank, duizend maal dank..

  2. Leo says:

    Geweldig, werkt perfect.
    Bedankt!

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>