简体   繁体   English

cURL文件上传到远程服务器

[英]cURL file upload to remote server

I want to upload a file to different server using cURL. 我想使用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: 当我运行此脚本时,它将继续运行200秒,并显示以下内容:

//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. 有趣的是,该文件出现在服务器上,但始终为320B,并包含难以理解的字符。 This is what server tolds me in logs: 这是服务器在日志中告诉我的内容:

Sep 17 20:56:58 xxxxx vsftpd[2613]: [user] OK LOGIN: Client "yyyyy" 9月17日20:56:58 xxxxx vsftpd [2613]:[用户]确定登录:客户端“ yyyyy”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "230 Login successful." 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 230登录成功”。

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "PBSZ 0" 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP命令:客户端“ yyyyy”,“ PBSZ 0”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "200 PBSZ set to 0." 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 200 PBSZ设置为0”。

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "PWD" 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP命令:客户端“ yyyyy”,“ PWD”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "257 "/"" 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 257” /”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "CWD directory" 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP命令:客户端“ yyyyy”,“ CWD目录”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "250 Directory successfully changed." 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 250目录已成功更改”。

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "EPSV" 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP命令:客户端“ yyyyy”,“ EPSV”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "229 Entering Extended Passive Mode (|||65024|)." 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 229进入扩展被动模式(||| 65024 |)。”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "TYPE I" 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP命令:客户端“ yyyyy”,“ TYPE I”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "200 Switching to Binary mode." 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 200切换到二进制模式”。

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP command: Client "yyyyy", "STOR received.txt" 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP命令:客户端“ yyyyy”,“ STOR receive.txt”

Sep 17 20:56:58 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "150 Ok to send data." 9月17日20:56:58 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 150确定可以发送数据”。

Sep 17 21:03:38 xxxxx vsftpd[2618]: [user] OK UPLOAD: Client "yyyyy", "/directory/received.txt", 320 bytes, 0.00Kbyte/sec 9月17日21:03:38 xxxxx vsftpd [2618]:[用户]确定上传:客户端“ yyyyy”,“ / directory / received.txt”,320字节,0.00KB /秒

Sep 17 21:03:38 xxxxx vsftpd[2618]: [user] FTP response: Client "yyyyy", "226 Transfer complete." 9月17日21:03:38 xxxxx vsftpd [2618]:[用户] FTP响应:客户端“ yyyyy”,“ 226传输完成”。

I'am not cURL expert but i think it is case of add/remove some options in code, any help is welcome. 我不是cURL专家,但我认为是在代码中添加/删除某些选项的情况,欢迎任何帮助。

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); 
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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