Recursively deleting directories via FTP
Posted on April 5th, 2004 in Code Repository | 9 Comments »
A very common task involves deleting a directory on a remote FTP server – unfortunately if this directory contains any files or folders it gets complicated quickly.
This function performs RAWLIST on the directory to obtain all the files, then deletes them iteratively – this saves memory, communication to the remote server, and is very speedy.
/**
* Recursively delete the files in a directory via FTP.
*
* @author Aidan Lister <aidan@php.net>
* @version 1.0.0
* @link http://aidanlister.com/2004/04/recursively-deleting-directories-via-ftp/
* @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);
}
9 Responses
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
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.
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.
(bad english:)Don’t know if I have “old server” in use. There is no effect if inner folders have less permissions like 644?
Hello,
Thanks for great function.
So cool.
im having some issues i put the directory but it tries to delete everything form outside the folder and then it goes into the folder and tries to find the folders that are actually outside the folder?
Unfortunately, on windows the recursive option of ftp_rawlist() does not work (at least not on my machine and Turigeza’s).
The function above can be altered a bit to be recursive. This solves the issue.
I can’t post the entire function, but here is the change:
Reverse the order of the files and folder handling and replace with:
// delete the subfolders recursively
foreach ($folders as $folder) {
self::ftp_rmdirr($ftp_stream, $folder);
}
// then delete all the files
foreach ($files as $file) {
ftp_delete($ftp_stream, $file);
}
Hope this helps – Monkhorst
excelent work;it’s working like a dream;thank you very much!