You are here

function drupal_ftp_delete_folder in Backup and Migrate 6.2

Same name and namespace in other branches
  1. 8.2 includes/destinations.ftp.inc \drupal_ftp_delete_folder()
  2. 8.3 includes/destinations.ftp.inc \drupal_ftp_delete_folder()
  3. 6.3 includes/destinations.ftp.inc \drupal_ftp_delete_folder()
  4. 7.3 includes/destinations.ftp.inc \drupal_ftp_delete_folder()
  5. 7.2 includes/destinations.ftp.inc \drupal_ftp_delete_folder()

The drupal_ftp_delete_folder Function This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $folder_name.

File

includes/destinations.ftp.inc, line 383
Functions to handle the FTP backup destination.

Code

function drupal_ftp_delete_folder($folder_name, &$ftp) {
  if (!@drupal_ftp_connect($ftp)) {
    return FALSE;
  }
  @ftp_chdir($ftp->__conn, $folder_name);
  $location = @ftp_pwd($ftp->__conn);
  $directories = array();
  $files = array();
  $dir_counter = 0;
  $file_counter = 0;
  $content = @ftp_nlist($ftp->__conn, ".");
  for ($i = 0; $i < sizeof($content); $i++) {

    // If we can change into this then it's a directory.
    // If not, it's a file
    if ($content[$i] != "." && $content[$i] != "..") {
      if (@ftp_chdir($ftp->__conn, $content[$i])) {

        // We have a directory
        $directories[] = $content[$i];
        $dir_counter++;
        @ftp_cdup($ftp->__conn);
      }
      else {

        // We have a file
        $files[] = $content[$i];
        $file_counter++;
      }
    }
  }
  for ($j = 0; $j < $file_counter; $j++) {
    @ftp_delete($ftp->__conn, $files[$j]);
  }
  for ($j = 0; $j < $dir_counter; $j++) {
    if ($directories[$j] != "." or $directories[$j] != "..") {
      $location = ftp_pwd($ftp->__conn);
      drupal_ftp_delete_folder($directories[$j], $ftp);
      @ftp_cdup($ftp->__conn);
      @ftp_rmdir($ftp->__conn, $directories[$j]);
    }
  }

  // Lastly, we change into the directory that we want to delete and see
  // if we can cdup. If we can, we delete it.
  @ftp_chdir($ftp->__conn, $folder_name);
  @ftp_cdup($ftp->__conn);
  @ftp_rmdir($ftp->__conn, $folder_name);

  // Did the recursive folder/file deletion work?
  return TRUE;
}