Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : FTP Functions : ftp_rmdir

ftp_rmdir

Removes a directory (PHP 4, PHP 5)
bool ftp_rmdir ( resource ftp_stream, string directory )

Example 746. ftp_rmdir() example

<?php

$dir
= 'www/';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to delete the directory $dir
if (ftp_rmdir($conn_id, $dir)) {
   echo
"Successfully deleted $dir\n";
} else {
   echo
"There was a problem while deleting $dir\n";
}

ftp_close($conn_id);

?>

Code Examples / Notes » ftp_rmdir

mgyth_at-online_dot-no

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.


mvh

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


tse-webdesign

The problem with the previous posts about recursive deleting of directories is that you might need to set error_reporting(0), but this doesn't work always.
When you've got your own error_handler set, you'll notice that an error is thrown even when setting error_reporting(0).
The following script generates various errors:
<?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);
  }
?>
Let me explain: with the @-sign you can suppres the error generated if the command failed, but somehow it still outputs the error.
So the easiest solution for this is to check if the path is to a file or a directory, which you can check with ftp_nlist or ftp_rawlist.
ftp_nlist(filename) returns the filename, ftp_nlist(dirname) returns a list with the files in that directory, or returns an empty array if the directory is empty.
If ftp_nlist is not working, you can use ftp_rawlist() and parse the results.
With ftp_rawlist(resource, path) you can get a detailed list of the files in a specific directory. At the end of every element of the array that ftp_rawlist(resource, path) returns, you'll find the filename or the dirname.
This file- or dirname you can precede with the path you've provided for ftp_rawlist(), which gives you the ability to check if a path to a file or directory is really a file or a directory.
This is what I came up with:
<?
/**
* Delete the provided directory and all its contents from the FTP-server.
*
* @param string $path Path to the directory on the FTP-server relative to the current working directory
*/
function DeleteDirRecursive ($resource, $path)
{
$list = ftp_nlist ($resource, $path);
if ( empty($nlist) ){
$list = RawlistToNlist ( ftp_rawlist($resource, $path), $path . ( substr($path, strlen($path) - 1, 1) == "/" ? "" : "/" ) );
}
if ($list[0] != $path){
$path .= ( substr($path, strlen($path)-1, 1) == "/" ? "" : "/" );
foreach ($list as $item){
if ($item != $path.".." && $item != $path."."){
DeleteDirRecursive ($item);
}
}
ftp_rmdir ($resource, $path);
}
else {
ftp_delete ($resource, $path);
}
}

/**
* Convert a result from ftp_rawlist() to a result of ftp_nlist()
*
* @param array $rawlist Result from ftp_rawlist();
* @param string $path Path to the directory on the FTP-server relative to the current working directory
* @return array An array with the paths of the files in the directory
*/
function RawlistToNlist ($rawlist, $path)
{
$array = array();
foreach ($rawlist as $item){
$filename = trim(substr($item, 55, strlen($item) - 55));
if ($filename != "." || $filename != ".."){
$array[] = $path . $filename;
}
}
return $array;
}
?>


uma

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;
}


turigeza on yahoo com

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

If you wish to recursively delete a directory and all its contents, see the below function.
http://aidanlister.com/repos/v/function.ftp_rmdirr.php


mail

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;
}


Change Language


Follow Navioo On Twitter
ftp_alloc
ftp_cdup
ftp_chdir
ftp_chmod
ftp_close
ftp_connect
ftp_delete
ftp_exec
ftp_fget
ftp_fput
ftp_get_option
ftp_get
ftp_login
ftp_mdtm
ftp_mkdir
ftp_nb_continue
ftp_nb_fget
ftp_nb_fput
ftp_nb_get
ftp_nb_put
ftp_nlist
ftp_pasv
ftp_put
ftp_pwd
ftp_quit
ftp_raw
ftp_rawlist
ftp_rename
ftp_rmdir
ftp_set_option
ftp_site
ftp_size
ftp_ssl_connect
ftp_systype
eXTReMe Tracker