WEB using curl to upload via ftp from form

Discussion in 'OT Technology' started by drpepper, Mar 16, 2009.

  1. drpepper

    drpepper Active Member

    Joined:
    Nov 13, 2006
    Messages:
    38,076
    Likes Received:
    2
    Location:
    San Antonio
    i realize you can do this to one ftp server, but can 1 upload be sent to multiple ftp servers?
     
  2. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
  3. drpepper

    drpepper Active Member

    Joined:
    Nov 13, 2006
    Messages:
    38,076
    Likes Received:
    2
    Location:
    San Antonio
    does the 1st recieving server need to be a webserver? or can it be just any server with ftp server software?
     
  4. drpepper

    drpepper Active Member

    Joined:
    Nov 13, 2006
    Messages:
    38,076
    Likes Received:
    2
    Location:
    San Antonio
    this is what i have now:

     
  5. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Yes, the first server would need to be a web server, with this code below whatever server is running the script would accept the upload and then send it off to however many other servers you want. Keep in mind that this would be a long page load for anyone who submits it since they have to wait for it to upload the file four times. I'd set up some sort of CRON to run and upload to the other servers after the fact.

    This is untested but I'd do something like:

    upload.php:
    Code:
    <form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    </form>
    <?
      
      /*
      #### Upload to your main server ####
      */
      
      $target_path = "uploads/";
      
      $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
      
      $fileName = $_FILES['uploadedfile']['name'];
      
      if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
          echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
          " has been uploaded";
      } else{
          echo "There was an error uploading the file, please try again!";
      }
          
      
      /*
      #### Upload to other/backup servers ####
      */
      
      ftpUpload($target_path, $fileName, 'ftp.server1.com', 'user', 'pass');
      ftpUpload($target_path, $fileName, 'ftp.server2.com', 'user', 'pass');
      ftpUpload($target_path, $fileName, 'ftp.server3.com', 'user', 'pass');
      
      
      
      /*
      #### FTP Upload function ####
      */
      
      function ftpUpload($fileFrom, $fileTo, $host, $user, $pass) {
        
        // set up basic connection
        $conn_id = ftp_connect($host);
        
        // login with username and password
        $login_result = ftp_login($conn_id, $user, $pass);
        
        // upload a file
        if (ftp_put($conn_id, $fileTo, $fileFrom, FTP_ASCII)) {
         echo "successfully uploaded $fileFrom\n";
        } else {
         echo "There was a problem while uploading $fileFrom\n";
        }
        
        // close the connection
        ftp_close($conn_id);
         
      }
       
    ?>
    
     

Share This Page