1

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:

According to one of this article answers if you need implicit, use curl: ftp_ssl_connect with implicit ftp over tls

1.The code:

$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).

Response says: false Also I'm getting error no:35 which is according to manual:

CURLE_SSL_CONNECT_ERROR (35)

A problem occurred somewhere in the SSL/TLS handshake. 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:

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. Not sure if that is of any help as we don't have ftp client on the server. FileZilla log when uploading the file:

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:

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.
Community
  • 1
  • 1
anonymous007
  • 319
  • 1
  • 3
  • 12

1 Answers1

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);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Rakesh Dongarwar
  • 475
  • 7
  • 10