簡體   English   中英

使用隱式FTP over TLS加密將PHP文件上傳到遠程FTP目錄:上傳的文件為空

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

大家好,我正在嘗試使用PHP通過TLS加密通過隱式FTP將文件上傳到遠程FTP目錄,這是我到目前為止已經嘗試過的方法:

根據本文答案之一,如果您需要隱式,請使用curl: ftp_ssl_connect和tls上的隱式ftp

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,這是根據手冊

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

服務器錯誤日志未記錄任何錯誤。

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