Delete folder recursively via php if ftp says: Prohibited directory name

Using eclipse to sync an ftp folder with my local XAMPP installation I ran into he following situation. I ended up with a folder created on the remote machine called 'C:\xampp\tmp' that I could not delete or even view properties or content of using any ftp client I could get hands on (filezilla, ncftp3, gnome ftp “server connect”). Since the folder was on webspace I thought: “Mmh, why not try php.” So I created a file called delfolder.php right in the folder containing the miscurious c:-directory and finally came up with this:


< ?php
function rm_recursive($filepath) {
    if (is_dir($filepath) && !is_link($filepath)) {
        if ($dh = opendir($filepath)) {
            while (($sf = readdir($dh)) !== false) {
                if ($sf == '.' || $sf == '..' ) {
                    continue;
                }
                if (!rm_recursive($filepath.'/'.$sf)) {
                    throw new Exception($filepath.'/'.$sf.' could not be deleted.');
                }
            }
            closedir($dh);
        }
        return rmdir($filepath);
    }
    return unlink($filepath);
}

// Path to directory you want to delete
$directory = 'C:\xampp\tmp';

// Delete it
if (rm_recursive($directory)) {
    echo "{$directory} has been deleted";
} else {
    echo "{$directory} could not be deleted";
}

This, of course, only works if you have either direct access via shell (but than again why not just use rm -Rf ‘C:\xampp\tmp’) or FTP and web, i.e. HTTP, access to the folder in question.

Resources:

Post a Comment