module for saving a node to ftp

  1. /**
  2. * Implementation of hook_form_alter. Define the form for saving to ftp.
  3. */     
  4.  
  5. function savetoftp_form_alter($form_id, &$form) {
  6.  if ($form_id == 'regpage_node_form') { 
  7.         $form['savetoftp'] = array(
  8.       '#type' => 'fieldset',
  9.       '#title' => t('FTP Server settings'),
  10.       '#collapsible' => TRUE,
  11.       '#collapsed' => FALSE,
  12.       '#access' => user_access('administer nodes'),
  13.       '#weight' => 30,
  14.     );
  15.     $form['savetoftp']['ftp_server'] = array(
  16.       '#type' => 'textfield',
  17.       '#default_value' => variable_get('ftp_server', 'www.sfsu.edu'),
  18.       '#maxlength' => 50,
  19.       '#collapsible' => TRUE,
  20.       '#collapsed' => TRUE,
  21.       '#description' => t('Enter your FTP Server'),
  22.     );
  23.         $form['savetoftp']['ftp_username'] = array(
  24.       '#type' => 'textfield',
  25.       '#default_value' => variable_get('ftp_username', ''),
  26.       '#maxlength' => 50,
  27.       '#collapsible' => TRUE,
  28.       '#collapsed' => TRUE,
  29.       '#description' => t('Enter your FTP Username'),
  30.     );
  31.         $form['savetoftp']['ftp_password'] = array(
  32.       '#type' => 'password',
  33.       '#default_value' => variable_get('ftp_password', 0),
  34.       '#maxlength' => 50,
  35.           '#size' => 15,
  36.       '#collapsible' => TRUE,
  37.       '#collapsed' => TRUE,
  38.       '#description' => t('Enter your FTP Password'),
  39.     );
  40.         $form['savetoftp']['ftp_directory'] = array(
  41.       '#type' => 'textfield',
  42.       '#default_value' => variable_get('ftp_directory', 'public_html'),
  43.       '#maxlength' => 50,
  44.       '#collapsible' => TRUE,
  45.       '#collapsed' => TRUE,
  46.       '#description' => t('Enter your default directory'),
  47.     );
  48.         $form['#submit'] = array(
  49.         'save_to_ftp' => array()
  50.         );
  51. }       
  52. }
  53.        
  54. function save_to_ftp($form, &$form_values) {
  55. /** Code added to save node to a file and then send it to ftp server*/
  56.         global $form_values;
  57.         global $node;
  58.         global $form;
  59.         print $form['body_filter']['body'];
  60.         print $form_values['body_filter']['body'];
  61.         print $node->body;
  62.         $fn = "mycustom.html";
  63.         $html_body = $form_values['body_filter']['body'];
  64.         print $html_body;
  65.         $file = fopen($fn, "w");
  66.         fwrite($file, $html_body);
  67.         fclose($file);
  68.         set_time_limit(0);
  69.         $ftpServer = $form_values['ftp_server'];
  70.         $ftp_uname = $form_values['ftp_username'];
  71.         $ftp_pass = $form_values['ftp_password'];
  72.         $targetDir = 'public_html/noindex/';
  73. if (!$fp = ftp_connect($ftpServer, 21, 30))
  74. {
  75.   die('Connection failed');
  76. }
  77. if (!ftp_login($fp, $ftp_uname, $ftp_pass))
  78. {
  79.   die('Login failed');
  80. }
  81. if (!ftp_chdir($fp, $targetDir))
  82. {
  83.   die ('Unable to change directory to: ' . $targetDir);
  84. }
  85.  
  86. $fileholder = "C:/web/xampp/htdocs/drupal-5.7/mycustom.html";
  87. $remote_file = "mycustom.html";
  88. $filetobemoved = fopen($fileholder,"r");
  89.                                                 if(!ftp_fput($fp, $remote_file, $filetobemoved, FTP_BINARY))
  90.                                                         {
  91.                                                         $errorMessage .= "&error-unable-ftp-fput;";
  92.                                                         }                                                      
  93. /** end of custom code add */
  94. }