【问题标题】:PHP file upload to a remote FTP directory using Implicit FTP over TLS encryption: uploaded file is empty使用基于 TLS 加密的隐式 FTP 将 PHP 文件上传到远程 FTP 目录:上传的文件为空
【发布时间】:2017-03-03 14:18:21
【问题描述】:

大家好,我正在尝试使用基于 TLS 加密的隐式 FTP 将使用 PHP 的文件上传到远程 FTP 目录,这是我迄今为止尝试过的:

根据this article 的答案之一,如果您需要隐式,请使用 curl: ftp_ssl_connect with implicit ftp over tls

1.代码:

$remote_filename='data.csv';
$path = 'php://memory';
$ftp_server = '0.0.0.0';
$ftp_port = 990;
$ftp_user = 'user';
$ftp_pass = 'pass';
$fp = fopen($path, 'rw+');
$ftp_url= 'ftps://'.$ftp_server.'/'.$remote_filename; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $ftp_url);
curl_setopt($ch, CURLOPT_PORT, $ftp_port);
curl_setopt($ch, CURLOPT_USERPWD,$ftp_user .':'.$ftp_pass );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);

$response = curl_exec($ch);
$error_no = curl_errno($ch); // error number
$error = curl_error($ch); // error buffer
//var_dump(curl_error($ch));
curl_close($ch);

但是这对我不起作用。远程 ftp 目录上的文件已创建但为空(大小为 0)。

回应说: 根据manual,我也收到错误号:35:

CURLE_SSL_CONNECT_ERROR (35)

SSL/TLS 握手中出现问题。您真的需要错误缓冲区并在那里阅读消息,因为它可以稍微定位问题。可以是证书(文件格式、路径、权限)、密码等。

查看了错误缓冲区,因为它显示了更多详细信息:

错误缓冲区说“遇到文件结尾”

我的问题: 有人知道如何解决这个问题吗?

更新 1 已将 curl 登录添加到上面的脚本中:

curl_setopt($ch, CURLOPT_VERBOSE, TRUE); // enable curl log
curl_setopt($ch, CURLOPT_STDERR, $fp); // curl log

,这里是日志内容:

* About to connect() to #.#.#.# port 990 (#0)
*   Trying #.#.#.#...
* Connected to #.#.#.# (#.#.#.#) port 990 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* skipping SSL peer certificate verification
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
*   subject: E=admin@website.com,OU=1.4,O=Company Ltd,ST=United Kingdom,C=44,CN=#.#.#.#
*   start date: Jan 05 13:31:18 2016 GMT
*   expire date: Jan 04 13:31:18 2017 GMT
*   common name: #.#.#.#
*   issuer: E=admin@website.com,OU=1.4,O=Company Ltd,ST=United Kingdom,C=44,CN=#.#.#.#
< 220-WELCOME TO THE Company Ltd FTP SERVER
< 220-
< 220-If you have any issues, please email IT@website.com
< 220-
< 220-Thank you
< 220 ------------------------
> USER #######
< 331 Password required for #######
> PASS #######
< 230 Logged on
> PBSZ 0
< 200 PBSZ=0
> PROT P
< 200 Protection level set to P
> PWD
< 257 "/" is current directory.
* Entry path is '/'
> EPSV
* Connect data stream passively
* ftp_perform ends with SECONDARY: 0
< 229 Entering Extended Passive Mode (|||55857|)
*   Trying #.#.#.#...
* Connecting to #.#.#.# (#.#.#.#) port 55857
* Connected to #.#.#.# (#.#.#.#) port 990 (#0)
> TYPE I
< 200 Type set to I
> STOR data_1.csv
< 150 Opening data channel for file upload to server of "/data_1.csv"
* Doing the SSL/TLS handshake on the data stream
* skipping SSL peer certificate verification
* NSS error -5938 (PR_END_OF_FILE_ERROR)
* Encountered end of file
* Failure sending ABOR command: SSL connect error
* Closing connection 0

更新 2 使用 Ftp 客户端 FileZilla 上传工作正常,但是我从我的计算机上传文件,而不是从我们正在运行 PHP 脚本的服务器上传文件。不确定这是否有帮助,因为我们在服务器上没有 ftp 客户端。 上传文件时的FileZilla日志:

Status: Starting upload of C:\data.csv
Command:    PASV
Response:   227 Entering Passive Mode (0,0,0,0,216,218)
Command:    STOR data.csv
Response:   150 Opening data channel for file upload to server of "/data.csv"
Response:   226 Successfully transferred "/data.csv"
Status: File transfer successful, transferred 251,542 bytes in 1 second
Status: Retrieving directory listing of "/"...
Command:    PASV
Response:   227 Entering Passive Mode (0,0,0,0,215,110)
Command:    MLSD
Response:   150 Opening data channel for directory listing of "/"
Response:   226 Successfully transferred "/"
Status: Directory listing of "/" successful

服务器错误日志没有记录任何错误。

更新 3 FileZilla 登录级别设置为 2(info),内容如下:

Status: Connecting to #.#.#.#:990...
Status: Connection established, initializing TLS...
Trace:  TLS Handshake successful
Trace:  Protocol: TLS1.2, Key exchange: ECDHE-RSA, Cipher: #, MAC: #
Status: Verifying certificate...
Status: TLS connection established, waiting for welcome message...
Response:   220-WELCOME TO THE COMPANY FTP SERVER
Response:   220-
Response:   220-If you have any issues, please email IT@company.com
Response:   220-
Response:   220-Thank you
Response:   220 ------------------------
Command:    USER ####
Response:   331 Password required for ####
Command:    PASS *************
Response:   230 Logged on
Command:    PBSZ 0
Response:   200 PBSZ=0
Command:    PROT P
Response:   200 Protection level set to P
Status: Connected
Trace:  Measured latency of 16 ms
Status: Starting upload of C:\data.csv
Command:    CWD /
Response:   250 CWD successful. "/" is current directory.
Command:    TYPE I
Response:   200 Type set to I
Command:    PASV
Response:   227 Entering Passive Mode (#,#,#,#,216,202)
Trace:  Binding data connection source IP to control connection source IP 192.168.1.50
Command:    STOR data.csv
Trace:  Trying to resume existing TLS session.
Trace:  TLS Handshake successful
Trace:  TLS Session resumed
Trace:  Protocol: TLS1.2, Key exchange: ECDHE-RSA, Cipher: #, MAC: #
Response:   150 Opening data channel for file upload to server of "/data.csv"
Response:   226 Successfully transferred "/data.csv"
Status: File transfer successful, transferred 251,542 bytes in 1 second
Status: Retrieving directory listing of "/"...
Command:    PASV
Response:   227 Entering Passive Mode (#,#,#,#,217,234)
Trace:  Binding data connection source IP to control connection source IP 192.168.1.50
Command:    MLSD
Trace:  Trying to resume existing TLS session.
Response:   150 Opening data channel for directory listing of "/"
Trace:  TLS Handshake successful
Trace:  TLS Session resumed
Trace:  Protocol: TLS1.2, Key exchange: ECDHE-RSA, Cipher: #, MAC: #
Response:   226 Successfully transferred "/"
Status: Directory listing of "/" successful
Status: Sending keep-alive command
Command:    TYPE I
Response:   200 Type set to I
Trace:  Skipping reply after cancelled operation or keepalive command.
Status: Sending keep-alive command
Command:    TYPE I
Response:   200 Type set to I
Trace:  Skipping reply after cancelled operation or keepalive command.

【问题讨论】:

  • Enable curl logging 并向我们展示日志。服务器端 FTP 日志也会有所帮助。您可以使用独立的 FTP 客户端(来自同一台机器)上传文件吗?
  • 刚刚用 curl 日志内容更新了我的问题?你能解释一下可能导致问题的原因吗?
  • 再次,您可以使用独立的 FTP 客户端(从同一台机器)上传文件吗?如果可以的话,也发布它的日志。 + 你可以访问服务器端日志吗?
  • 我应该查看哪个服务器端日志?如果 error_log 则在上传过程中不会记录任何错误。
  • 我的意思是 FTP 服务器日志 + 请将 FileZilla 日志级别提高到“2 - 信息”。

标签: php upload ftp implicit


【解决方案1】:
$ftpServer = "xxxx.xxxx.xxxx";
$portNo = "990";
$username = "xxxxxxx";
$password = "xxxxxxx";
$local_file = "Local Directoy Path/filename;
$ftp_path = "FTP Directoy Path/filename";

$fp = fopen($local_file, 'rw+');
$ftp_url= 'ftps://'.$ftpServer.'/'.$ftp_path; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $ftp_url);
curl_setopt($ch, CURLOPT_PORT, $portNo);
curl_setopt($ch, CURLOPT_USERPWD,$username .':'.$password );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);

$response = curl_exec($ch);
$error_no = curl_errno($ch); // error number
$error = curl_error($ch); // error buffer
//var_dump(curl_error($ch));
curl_close($ch);

【讨论】:

  • 你能解释一下你的答案与我在我的问题中已经写的有什么不同吗?
  • $ftp_path = "FTP 目录路径/文件名"; $fp = fopen($local_file, 'rw+');而不是 'php://memory';指定本地机器目录路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多