大家好我试图使用隐式FTP over TLS加密将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)。
回复说: false 我还得到错误号码:35,这符合manual:
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(信息),内容如下:
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.
答案 0 :(得分:0)
$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);