Guest User

Untitled

a guest
Apr 10th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. */
  6. class FtpTransfer
  7. {
  8.  
  9. private $user;
  10. private $password;
  11. private $ftp_server;
  12. private $remote_dir;
  13. private $ftp_string;
  14.  
  15. function __construct($server, $user, $password){
  16. $this->ftp_server = $server;
  17. $this->user = $user;
  18. $this->password = $password;
  19. $this->ftp_string = "ftp://$this->user:$this->password@$this->ftp_server";
  20. }
  21.  
  22. function set_remote_dir($path){
  23. $this->remote_dir = $path;
  24. }
  25.  
  26. function upload_curl($filename){
  27. if(file_exists($filename)){
  28. $ch = curl_init();
  29. $fp = fopen($filename, 'r');
  30. $remote_file = $this->$remote_dir.$filename;
  31. //echo "sending $remote_file to $ftp_string$remote_file\n";
  32. curl_setopt($ch, CURLOPT_URL, $this->ftp_string.$remote_file);
  33. curl_setopt($ch, CURLOPT_UPLOAD, 1);
  34. curl_setopt($ch, CURLOPT_INFILE, $fp);
  35. curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
  36. curl_exec ($ch);
  37. $error_no = curl_errno($ch);
  38. curl_close ($ch);
  39. if ($error_no == 0) {
  40. $error = "File $filename uploaded succesfully.\n";
  41. } else {
  42. $error = "File $filename upload error. \n";
  43. }
  44. echo "$error";
  45. }else{
  46. echo "The file doesn't exists...$filename\n";
  47. }
  48. }
  49. }
  50.  
  51. //EXAMPLEE
  52. $ftp_server = "FTP_SERVER";
  53. $ftp_user_name = "USER_NAME";
  54. $ftp_password = "PASSWORD";
  55. $remote_dir = "REMOTE_DIR";
  56.  
  57. $ftpTransfer = new FtpTransfer($ftp_server, $ftp_user_name, $ftp_password);
  58. $ftpTransfer->set_remote_dir = "REMOTE_DIR";
  59. $ftpTransfer->upload_curl("FILE_PATH");
  60.  
  61.  
  62.  
  63.  
  64. ?>
Add Comment
Please, Sign In to add comment