简体   繁体   English

使用隐式FTP over TLS加密将PHP文件上传到远程FTP目录:上传的文件为空

[英]PHP file upload to a remote FTP directory using Implicit FTP over TLS encryption: uploaded file is empty

Hi everyone i'm trying to upload file using PHP to a remote FTP directory using Implicit FTP over TLS encryption, this is what i have tried so far: 大家好,我正在尝试使用PHP通过TLS加密通过隐式FTP将文件上传到远程FTP目录,这是我到目前为止已经尝试过的方法:

According to one of this article answers if you need implicit, use curl: ftp_ssl_connect with implicit ftp over tls 根据本文答案之一,如果您需要隐式,请使用curl: ftp_ssl_connect和tls上的隐式ftp

1.The code: 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);

However this is not working for me. 但是,这对我不起作用。 File on the remote ftp directory is created but is empty (0 in size). 远程ftp目录上的文件已创建,但为空(大小为0)。

Response says: false Also I'm getting error no:35 which is according to manual : 响应说: false我也收到错误号:35,这是根据手册

CURLE_SSL_CONNECT_ERROR (35) CURLE_SSL_CONNECT_ERROR(35)

A problem occurred somewhere in the SSL/TLS handshake. SSL / TLS握手中的某处发生了问题。 You really want the error buffer and read the message there as it pinpoints the problem slightly more. 您确实想要错误缓冲区并在此处阅读消息,因为它可以更精确地指出问题所在。 Could be certificates (file formats, paths, permissions), passwords, and others. 可以是证书(文件格式,路径,权限),密码和其他。

Have looked into error buffer as it shows more details: 研究了错误缓冲区,因为它显示了更多详细信息:

Error buffer is saying "Encountered end of file" 错误缓冲区说“遇到文件结尾”

My question: Does anyone know how to solve this problem? 我的问题: 有人知道如何解决这个问题吗?

UPDATE 1 Have added curl logging into script above: 更新1在上面的脚本中添加了curl登录:

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

, and here is the log content: ,这是日志内容:

* 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

UPDATE 2 When using Ftp Client FileZilla upload works fine, however i have uploaded file from my computer not from the server we are running the PHP script. 更新2当使用Ftp客户端FileZilla上传工作正常,但是我从我的计算机而不是从我们正在运行PHP脚本的服务器上传文件。 Not sure if that is of any help as we don't have ftp client on the server. 不知道这是否有帮助,因为我们服务器上没有ftp客户端。 FileZilla log when uploading the file: 上载文件时的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

Server error log is not logging any error. 服务器错误日志未记录任何错误。

UPDATE 3 FileZilla loggin level set to 2(info) and the content is below: UPDATE 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.
$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);

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

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