Aidan's PHP Repository

A repository for PHP functions and classes ...

function.ftp_rmdirr.php

Recursively delete the files in a directory via FTP.

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.0.0
  • Link: http://aidanlister.com/repos/v/function.ftp_rmdirr.php
  • Views: 7381
  • Downloads: 1594

Source

Download this script <?php
/**
 * Recursively delete the files in a directory via FTP.
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.0
 * @link        http://aidanlister.com/repos/v/function.ftp_rmdirr.php
 * @param       resource $ftp_stream   The link identifier of the FTP connection
 * @param       string   $directory    The directory to delete
 */
function ftp_rmdirr($ftp_stream, $directory)
{
    // Sanity check
    if (!is_resource($ftp_stream) ||
        get_resource_type($ftp_stream) !== 'FTP Buffer') {
 
        return false;
    }
 
    // Init
    $i             = 0;
    $files         = array();
    $folders       = array();
    $statusnext    = false;
    $currentfolder = $directory;
 
    // Get raw file listing
    $list = ftp_rawlist($ftp_stream, $directory, true);
 
    // Iterate listing
    foreach ($list as $current) {
        
        // An empty element means the next element will be the new folder
        if (empty($current)) {
            $statusnext = true;
            continue;
        }
 
        // Save the current folder
        if ($statusnext === true) {
            $currentfolder = substr($current, 0, -1);
            $statusnext = false;
            continue;
        }
 
        // Split the data into chunks
        $split = preg_split('[ ]', $current, 9, PREG_SPLIT_NO_EMPTY);
        $entry = $split[8];
        $isdir = ($split[0]{0} === 'd') ? true : false;
 
        // Skip pointers
        if ($entry === '.' || $entry === '..') {
            continue;
        }
 
        // Build the file and folder list
        if ($isdir === true) {
            $folders[] = $currentfolder . '/' . $entry;
        } else {
            $files[] = $currentfolder . '/' . $entry;
        }
 
    }
 
    // Delete all the files
    foreach ($files as $file) {
        ftp_delete($ftp_stream, $file);
    }
 
    // Delete all the directories
    // Reverse sort the folders so the deepest directories are unset first
    rsort($folders);
    foreach ($folders as $folder) {
        ftp_rmdir($ftp_stream, $folder);
    }
 
    // Delete the final folder and return its status
    return ftp_rmdir($ftp_stream, $directory);
}
 
?>

Comments

August 7th, 2006
Hello, Thanks for great function.
January 31st, 2006
(bad english:)Don't know if I have "old server" in use. There is no effect if inner folders have less permissions like 644?
November 20th, 2005
It seems that some FTP servers don't support the recursive raw listing. From what I can tell, this is a small number of old servers. In these cases, the function above won't work.
November 8th, 2005
to add I'm on Windows XP and PHP 5.0.4 an it seems the recursive parameter for the ftp_rawlist() makes no diffrence for me ?? so that's why your function didn't work.
November 8th, 2005
Doesn't recursive mean that the function will call itself ?? This function doesn't work for me :( folder/folder1/folder2/folder3/folder4/ ftp_rmdirr($ftp_stream, 'folder/folder1') won't delete folder1 and says directory is not empty