Reputation: 69
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...
Basically what I am trying to do is the following:
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.
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...
Here's what I have so far...
Any help is much appreciated and I do apologize if this is answered some where else I just didn't see it! :)
AS AN UPDATE TO THIS I JUST THOUGHT OF SOMETHING...
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?
update.php
$updater = new updater($accountInfo['ftp_user'],$accountInfo['ftp_pass'],$accountInfo['host']);
$file = "relative/path/to/my/file";
$source = $updater->getFile($file);
$updater->update($source,$file);
my update class
class updater {
var $ftp_user = "";
var $ftp_pass = "";
var $ftp_host = "";
function __construct($user,$pass,$host) {
$this->ftp_user = $user;
$this->ftp_pass = $pass;
$this->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."<BR>");
echo("File: ".$file."<BR>");
$connection = ftp_connect($this->ftp_host);
$login = ftp_login($connection, $this->ftp_user, $this->ftp_pass);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, "/home/".$this->ftp_host."/public_html/".$file, $source, FTP_BINARY);
if (!$upload) {
echo 'FTP upload failed!';
} else {
echo("UPDATED FILE ".$source." SUCCESSFULLY!<BR>");
}
ftp_close($connection);
}
Upvotes: 1
Views: 2044
Reputation: 6097
Try this:
# Declare Files
$local_file = "/home/user/public_html/file.php";
$remote_file = "public_html/remote_file.php";
# FTP
$server = "ftp://ftp.yourhost.com/".$remote_file;
# FTP Credentials
$ftp_user = "ftp_username";
$ftp_password = "password";
# Upload File
$ch = curl_init();
$ftp_file = fopen($local_file, 'r');
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_USERPWD, $ftp_user.":".$ftp_password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $ftp_file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));
$result = curl_exec($ch);
You can easily put it in a function if this does what you want.
Upvotes: 2