title: Getting a file from my local server and uploading it to a remote server using php
body: <p>I know there are many posts about this issue however I cannot find one single answer on here that describes EXACTLY what I am trying to do and I have been searching for HOURS...so hopefully you all can help me and steer me in the right direction, the main thing I cannot figure out how to do is get the source file and pass that to my update function where I will ftp that file to the remote server...</p>
  
  <p>Basically what I am trying to do is the following:</p>
  
  <p>1) Get a file from my local host (ex. /home/user/public_html/file.php)
  2) Upload that file to a remote server of one of my clients (I have all of their FTP information stored in my database and can connect to their server just fine)
  3) If the file exists on the remote server, have it over write with the one.</p>
  
  <p>Now, I have tried ftp_get to get the file but that is not what I want to do because I don't need to re-write or store the same file to the local server...</p>
  
  <p>Here's what I have so far...</p>
  
  <p>Any help is much appreciated and I do apologize if this is answered some where else I just didn't see it! :)</p>
  
  <p><strong>AS AN UPDATE TO THIS I JUST THOUGHT OF SOMETHING...</strong></p>
  
  <p>Why do I even have to get the local file via ftp? Shouldn't I just be able to get the local file some other way and pass that to my update function where it will upload that file to the remote server?</p>
  
  <p><strong>update.php</strong></p>
  
  <pre><code>$updater = new updater($accountInfo['ftp_user'],$accountInfo['ftp_pass'],$accountInfo['host']);
  $file = "relative/path/to/my/file";
  $source = $updater-&gt;getFile($file);
  $updater-&gt;update($source,$file);
  </code></pre>
  
  <p><strong>my update class</strong></p>
  
  <pre><code>class updater {
  var $ftp_user = "";
  var $ftp_pass = "";
  var $ftp_host = "";
  
  function __construct($user,$pass,$host) {
  $this-&gt;ftp_user = $user;
  $this-&gt;ftp_pass = $pass;
  $this-&gt;ftp_host = $host;    
  }
  
  
  
   function getFile($file) {
  $curl = curl_init();
  $file = fopen($file, 'w');
  curl_setopt($curl, CURLOPT_URL, "ftp://domain.com/".$file); #input
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  //curl_setopt($curl, CURLOPT_FILE, $file); #output
  curl_setopt($curl, CURLOPT_USERPWD, "ftpuser:ftppass");
  $returnFile = curl_exec($curl);
  return $returnFile;
  }
  
  function update($source,$file) {
      echo("Source: ".$source."&lt;BR&gt;");
      echo("File: ".$file."&lt;BR&gt;");
  $connection = ftp_connect($this-&gt;ftp_host);
  
  $login = ftp_login($connection, $this-&gt;ftp_user, $this-&gt;ftp_pass);
  
  if (!$connection || !$login) { die('Connection attempt failed!'); }
  
  $upload = ftp_put($connection, "/home/".$this-&gt;ftp_host."/public_html/".$file, $source, FTP_BINARY);
  
  if (!$upload) { 
  echo 'FTP upload failed!'; 
  } else {
  echo("UPDATED FILE ".$source." SUCCESSFULLY!&lt;BR&gt;"); 
  }
  
  ftp_close($connection);     
  }
  </code></pre>