PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<ftp_renameftp_set_option>
view the version of this page
Last updated: Thu, 21 Aug 2003

ftp_rmdir

(PHP 3>= 3.0.13, PHP 4 )

ftp_rmdir -- Removes a directory

Description

bool ftp_rmdir ( resource ftp_stream, string directory)

Removes the specified directory. directory must be either an absolute or relative path to an empty directory.

Returns TRUE on success or FALSE on failure.

Example 1. ftp_rmdir() example

<?php

$dir = 'www/';

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (ftp_rmdir($conn_id, $dir)) {
    echo 'Successfully deleted ' . $dir;
} else {
    echo 'There was a problem while deleting ' . $dir;
}

ftp_close($conn_id);

?>

See also ftp_mkdir().



add a note add a note User Contributed Notes
ftp_rmdir
lucas at rufy dot com
04-Aug-2003 03:51
The following function has been fully tested to recursively delete files through FTP. It is based on a previous post which did not fully work as posted. See http://rufy.com/tech/archives/000026.html for more information.

NOTE: use full path name for the destination directory

function ftp_rmAll($conn_id,$dst_dir){
    $ar_files = ftp_nlist($conn_id, $dst_dir);
    if (is_array($ar_files)){ // makes sure there are files
        for ($i=0;$i<sizeof($ar_files);$i++){ // for each file
            $st_file = $ar_files[$i];
            if (ftp_size($conn_id, $st_file) == -1){ // check if it is a directory
                ftp_rmAll($conn_id, $st_file); // if so, use recursion
            } else {
                ftp_delete($conn_id, $st_file); // if not, delete the file
            }
        }
    }
    ftp_rmdir($conn_id, $dst_dir); // delete empty directories
}
admin at xliners dot org
09-Mar-2003 05:26
For the example above this message, you also have to ignore the folders '.' and '..' or there will be an infinite clause.
.
.
.
foreach($file_list as $dir) {
    if($dir != "." && $dir != "..") {
          if(ftp_size($ftp_site,$dir)===-1){        // dirname
          ftp_rmAll($ftp_site,$dir);        // recursion
     }else        {
          ftp_delete($ftp_site,$dir);        // del file
     }
}
.
.
.
Hope it helps someone...
victor59 at mail dot hongkong dot com
11-Aug-2002 08:52
<?php
function ftp_rmAll(&$ftp_site,$dir){
 
$root=$dir;    // tmp variable

 
if(@!ftp_chdir($ftp_site,$root)){ // enter the DIRCTORY $root
   
echo 'Directory not found.';
    return 
0;
 }

 if(!empty(
$dir) && ($file_list=ftp_nlist($ftp_site,'.'))!=&& $file_list!=NULL)
 {
   
// list content inside the DIRECTORY $root
   
foreach($file_list as $dir) {
        if(
ftp_size($ftp_site,$dir)===-1){    // dirname
           
ftp_rmAll($ftp_site,$dir);    // recursion
       
}else    {
           
ftp_delete($ftp_site,$dir);    // del file
       
}
    }
 }

 
ftp_chdir($ftp_site,'../');    // dir is empty now, move one level up
 
if(ftp_rmdir($ftp_site,$root))    // remove the empty dir
   
return 1;
 return 
0;
}
?>

This one works well on php4.2.1 + IIS 5.0 + Win 2000 Server
NOTE: $file_list!=NULL is needed in IIS platform
29-May-2002 03:24
The above does not work correctly. This should:

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);
            }
        }
    }
    ftp_rmdir($conn, $dirName);
}

NOTE: make sure that $dirname = "the full path of the file or directory".

<ftp_renameftp_set_option>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST