Skip to content

Instantly share code, notes, and snippets.

@godie
Created April 2, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godie/51ea3c5e0640b74c7be8152b7f504608 to your computer and use it in GitHub Desktop.
Save godie/51ea3c5e0640b74c7be8152b7f504608 to your computer and use it in GitHub Desktop.
<?php
/**
*
*/
class FtpTransfer
{
private $user;
private $password;
private $ftp_server;
private $remote_dir;
private $ftp_string;
function __construct($server, $user, $password){
$this->ftp_server = $server;
$this->user = $user;
$this->password = $password;
$this->ftp_string = "ftp://$this->user:$this->password@$this->ftp_server";
}
function set_remote_dir($path){
$this->remote_dir = $path;
}
function upload_curl($filename){
if(file_exists($filename)){
$ch = curl_init();
$fp = fopen($filename, 'r');
$remote_file = $this->$remote_dir.$filename;
//echo "sending $remote_file to $ftp_string$remote_file\n";
curl_setopt($ch, CURLOPT_URL, $this->ftp_string.$remote_file);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = "File $filename uploaded succesfully.\n";
} else {
$error = "File $filename upload error. \n";
}
echo "$error";
}else{
echo "The file doesn't exists...$filename\n";
}
}
}
//EXAMPLEE
$ftp_server = "FTP_SERVER";
$ftp_user_name = "USER_NAME";
$ftp_password = "PASSWORD";
$remote_dir = "REMOTE_DIR";
$ftpTransfer = new FtpTransfer($ftp_server, $ftp_user_name, $ftp_password);
$ftpTransfer->set_remote_dir = "REMOTE_DIR";
$ftpTransfer->upload_curl("FILE_PATH");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment