简体   繁体   中英

cURL file upload to remote server

I want to upload a file to different server using cURL. I've read many similar issues but i didnt get any solution for my problem. Here's the code i've got for now:

  ftp_server = 'ftps://xxxxx.com';
  $ftp_user = 'user';
  $ftp_password = 'password';
  $ftp_certificate = 'cert.pem';     
  $source_file = 'tosend.txt';
  $destination_file = '/directory/received.txt';

  if(!file_exists($source_file)) {
     die('no file');
  }

  $file = fopen($source_file, 'r');

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
  curl_setopt($ch, CURLOPT_URL, $ftp_server . $destination_file);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERPWD, $ftp_user . ':' . $ftp_password);
  curl_setopt($ch, CURLOPT_TIMEOUT, 200);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 200);

  curl_setopt($ch, CURLOPT_UPLOAD, 1);
  curl_setopt($ch, CURLOPT_INFILE, $file);
  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($source_file));

  //SSL stuff
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_CAINFO, $ftp_certificate);

  //curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
  //curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);

  $upload_result = curl_exec($ch);
  $upload_info = curl_getinfo($ch);
  $error_no = curl_errno($ch);
  $error_msg = curl_error($ch);

  curl_close ($ch);
  fclose($file);

  if ($error_no == 0) {
      echo 'File uploaded succesfully.';
  } else {
      echo 'File upload error:' . $error_msg . ' | ' . $error_no;
  }


  var_dump($upload_info); //debug


  die('the end');

When i run this script it keep running for 200 secs and response with:

//error_no
File upload error:SSL connection timeout | 28
//curl_getinfo
array(19) { 
["url"]=> string(49) "ftps://xxxxx.com/directory/received.txt"
["http_code"]=> int(150)
["header_size"]=> int(0)
["request_size"]=> int(0)
["filetime"]=> int(-1)
["ssl_verify_result"]=> int(0)
["redirect_count"]=> int(0)
["total_time"]=> float(200.099361)
["namelookup_time"]=> float(0.002149)
["connect_time"]=> float(0.003154)
["pretransfer_time"]=> float(0.110952)
["size_upload"]=> float(0)
["size_download"]=> float(0)
["speed_download"]=> float(0)
["speed_upload"]=> float(0)
["download_content_length"]=> float(0)
["upload_content_length"]=> float(0)
["starttransfer_time"]=> float(0)
["redirect_time"]=> float(0)
}
//die
the end

The funny thing is that this file appear on the server but always have 320B and contain unintelligible characters. This is what server tolds me in logs:

Sep 17 20:56:58 xxxxx vsftpd[2613]: [user] OK LOGIN: Client "yyyyy"

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "230 Login successful."

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "PBSZ 0"

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "200 PBSZ set to 0."

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "PWD"

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "257 "/""

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "CWD directory"

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "250 Directory successfully changed."

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "EPSV"

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "229 Entering Extended Passive Mode (|||65024|)."

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "TYPE I"

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "200 Switching to Binary mode."

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "STOR received.txt"

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "150 Ok to send data."

Sep 17 21:03:38 xxxxx vsftpd[2618]: [user] OK UPLOAD: Client "yyyyy", "/directory/received.txt", 320 bytes, 0.00Kbyte/sec

Sep 17 21:03:38 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "226 Transfer complete."

I'am not cURL expert but i think it is case of add/remove some options in code, any help is welcome.

Try to add and test this options:

curl_setopt($ch, CURLOPT_PORT, 990); // port for ftp ssl
...
curl_setopt($ch, CURLOPT_USE_SSL, TRUE);
...
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); 
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM