ftp_rmdir

(PHP 3 >= 3.0.13, PHP 4, PHP 5)

ftp_rmdir -- 删除一个目录

说明

bool ftp_rmdir ( resource ftp_stream, string directory )

删除由参数 directory 指定的目录。directory 必须是一个空目录的绝对或相对路径。

如果成功则返回 TRUE,失败则返回 FALSE

例子 1. ftp_rmdir() 例子

<?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);
?>

参见 ftp_mkdir()


add a note add a note User Contributed Notes
mail at martinauer dot net
15-May-2006 01:36
At least in PHP 4.3.11 ftp_nlist lists complete paths, not just filenames. Therefore the function by uma at di4 dot com did not work for me. But it worked with a small change in line 6 like this:

function ftp_rmAll($conn_id,$dst_dir){
   $ar_files = ftp_nlist($conn_id, $dst_dir);
   //var_dump($ar_files);
   if (is_array($ar_files)){ // makes sure there are files
       for ($i=0;$i<sizeof($ar_files);$i++){ // for each file
           $st_file = basename($ar_files[$i]);
           if($st_file == '.' || $st_file == '..') continue;
           if (ftp_size($conn_id, $dst_dir.'/'.$st_file) == -1){ // check if it is a directory
               ftp_rmAll($conn_id,  $dst_dir.'/'.$st_file); // if so, use recursion
           } else {
               ftp_delete($conn_id,  $dst_dir.'/'.$st_file); // if not, delete the file
           }
       }
   }
   $flag = ftp_rmdir($conn_id, $dst_dir); // delete empty directories
   //return $flag;
}
uma at di4 dot com
07-Mar-2006 08:06
Removing a directory using FTP(modified Version):
___________________________________________

function ftp_rmAll($conn_id,$dst_dir){
   $ar_files = ftp_nlist($conn_id, $dst_dir);
   //var_dump($ar_files);
   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($st_file == '.' || $st_file == '..') continue;
           if (ftp_size($conn_id, $dst_dir.'/'.$st_file) == -1){ // check if it is a directory
               ftp_rmAll($conn_id,  $dst_dir.'/'.$st_file); // if so, use recursion
           } else {
               ftp_delete($conn_id,  $dst_dir.'/'.$st_file); // if not, delete the file
           }
       }
   }
   $flag = ftp_rmdir($conn_id, $dst_dir); // delete empty directories
   //return $flag;
}
mvh at list dot ru
02-Mar-2006 09:02
to: turigeza on yahoo com
You have made a mistake in the function. In it parts:
<?php
foreach($list as $value)
  
ftp_rmdirr($value, $handle);
?>

Need:
<?php
foreach($list as $value)
{
   if (
$value != $path . '/.' && $value != $path . '/..')
      
ftp_rmdirr($value, $handle);
}
?>
mgyth_at-online_dot-no
24-Feb-2006 03:19
Worth beeing aware of:
ftp_rmdir scrips that are porly written and require you to supply a $dir to delete, may in some cases, where the $dir is empty, run the script on the your base dir on the server, thus deleting all the files you have ftp acces to.
turigeza on yahoo com
07-Nov-2005 11:15
Recursive directory delete using ftp_nlist() ...
<?php
function ftp_rmdirr($path, $handle)
   {
   if (!(@
ftp_rmdir($handle, $path) || @ftp_delete($handle, $path)))
       {
      
$list = ftp_nlist($handle, $path);
       if (!empty(
$list))
           foreach(
$list as $value)
              
ftp_rmdirr($value, $handle);
       }
   @
ftp_rmdir($handle, $path);
   }
?>
aidan at php dot net
29-Jul-2005 05:28
If you wish to recursively delete a directory and all its contents, see the below function.

http://aidan.dotgeek.org/lib/?file=function.ftp_rmdirr.php