View Full Version : Remove directory recursively

01-19-2004, 04:38 AM
Hi all,

I have a problem with a function I got from PHP.net. The function deletes the files in a directory then deletes the empty directory. My problem is that I sometimes have directories 2-levels deep and wants to delete all files and folders from a selected directory and this function will return me an object error when I do so.

Here's the function:-

function removedirR($conn,$dirName){

$ar_files = ftp_nlist($conn, $dirName);
if (is_array($ar_files)){
for ($i=0;$i<count($ar_files);$i++){
$st_file = $ar_files[$i];
if (ftp_size($conn, $st_file) == -1){
$this->removedirR( $st_file);
} else {
ftp_delete($conn, $st_file);
if (ftp_rmdir($conn, $dirName))
return true;

Can someone points me in the correct direction? I attempted using ftp_res but I'm stuck on where to add the check into the above function.


01-20-2004, 05:30 AM
I've managed to get the above function to recursively delete directories and files within.

Can someone tell me if
(ftp_size($conn, $st_file) == -1) is a check on whether the said file is a directory? Coz I tried on one server and it actually tried to delete files when it should have deleted the directory, I got the no such directory error.

Another server I tried, had no such problem and I could delete folders, files etc fine. Is there any additional validation I can do to check the array item whether its a file or a directory?