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"; } [/sourcecode] 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:

4 Comments

  1. Dan said,

    Wednesday, 17th Mar 2010 at 22:54

    This is Great!
    Only script I have found that works, even over on drive.
    Thanks,

  2. Michael said,

    Thursday, 20th Aug 2009 at 01:54

    Of all the recursive directory delete scripts, this is the only one that worked for me! Thank you!!!

  3. Tommy said,

    Monday, 17th Aug 2009 at 13:18

    This was a great way to approach the problem with files and folders on a ftp server.

  4. kamal said,

    Tuesday, 23rd Dec 2008 at 06:13

    Hi

    How to delete the ftp file using php

    i gave a directory path ex:http://www.te.com/test ->test is a folder
    but here not deleted HTTP does not allow unlinking

    Thanks & Regards
    kamal.


Leave a reply to kamal Cancel reply