当前位置: 首页 > 知识库问答 >
问题:

cURL文件上载到远程服务器

李甫
2023-03-14

我想上传一个文件到不同的服务器使用卷曲。我读过许多类似的问题,但我没有得到任何解决我的问题。下面是我现在得到的代码:

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

当我运行此脚本时,它将保持运行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

Sep 17 20:56:58 xxxxx vsftpd[2613]:[user]确定登录:客户端“yyyyy”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[用户]FTP响应:客户端“YYYYY”,“230登录成功”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[user]FTP命令:Client“yyyyy”,“pbsz 0”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[用户]FTP响应:客户端“YYYYY”,“200 PBSZ设置为0。”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[user]FTP命令:Client“yyyyy”,“pwd”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[用户]FTP响应:客户端“YYYYY”,“257”/“”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[user]FTP命令:Client“yyyyy”,“cwd directory”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[用户]FTP响应:客户端“YYYYY”,“250目录已成功更改”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[user]FTP命令:Client“yyyyy”,“epsv”

Sep 17 20:56:58 xxxxx VSFTPD[2618]:[用户]FTP响应:客户端“YYYYY”,“229进入扩展被动模式(65024)”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[user]FTP命令:Client“yyyyy”,“type i”

Sep 17 20:56:58 xxxxx VSFTPD[2618]:[用户]FTP响应:客户端“YYYYY”,“200切换到二进制模式。”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[user]FTP命令:Client“yyyyy”,“stor received.txt”

Sep 17 20:56:58 xxxxx vsftpd[2618]:[用户]FTP响应:客户端“YYYYY”,“150发送数据Ok。”

Sep 17 21:03:38 xxxxx vsftpd[2618]:[user]确定上载:客户端“yyyyy”,“/directory/received.txt”,320字节,0.00千字节/秒

Sep 17 21:03:38 xxxxx vsftpd[2618]:[用户]FTP响应:客户端“YYYYY”,“226传输完成。”

我不是卷曲专家,但我认为这是添加/删除一些选项在代码的情况下,任何帮助是欢迎的。

共有1个答案

朱翔
2023-03-14

尝试添加并测试此选项:

curl_setopt($ch, CURLOPT_PORT, 990); // port for ftp ssl
...
curl_setopt($ch, CURLOPT_USE_SSL, TRUE);
...
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); 
...
 类似资料:
  • 我需要创建一个 Servlet,它提供来自外部服务器的文件并将其发送给用户。 因为我不希望servlet成为“中间人”,所以我需要它来服务文件,而不会将其预先传输给自己,然后重新提交给用户以解决性能问题(否则每次下载至少需要2倍它需要的时间-不谈论这些服务器之间的速度瓶颈。 这有可能吗? 谢谢!

  • 在Django python服务器上,我定制了一个用户可以上传文件的URL。现在的问题是,当我点击浏览器时,我能够成功地上传文件,但当我使用curl尝试同样的事情时,我无法做到这一点。 意见。派克 ........ ........ ........ ........ 名单。html 在浏览器上 在终点站,我试过了 我尝试了一些其他的变化,但似乎没有成功。我还尝试了一些其他命令,它们给出了“无cs

  • 如何将文件发送到WebService?为了上传文件,我需要使用multipart/form-data;我想知道如何使用PHP中的curl来实现这一点。 我的代码是: 服务器上的故障是: 我怎样才能解决这个问题?

  • 问题内容: 是否可以使用cURL部分下载远程文件?假设远程文件的实际文件大小为1000 KB。如何仅下载其中的前500 KB? 问题答案: 您还可以使用php-curl扩展名设置range标头参数。 但是如前所述,如果服务器不遵循该标头而是发送整个文件,则curl将下载所有文件。例如,http: //www.php.net忽略标题。但是,您可以(另外)设置一个写函数回调,并在收到更多数据时中止请求

  • 我正在尝试通过Multer将文件上载到服务器。这段代码所做的只是创建文件夹,而不是文件。我还尝试使用github上的教程,但在前端我的代码也没有成功 代码AJAX 还有MAIN.JS 我从前端获得的console.log输出:

  • 我正在运行一个tomcat服务器, 中,我导出如下所示。 < code > JAVA _ OPTS = " $ JAVA _ OPTS-JAVA agent:/opt/jaco co/lib/jaco agent . jar = dest file =/tmp/jaco co . exec,append=true,includes=*" 这将生成jacoco。文件夹中同一台计算机中的exec文件。