PHP FTPS+cURL

Using FTPS (SSL) with PHP

Page 1 of 1

5 Replies - 14331 Views - Last Post: 05 July 2012 - 09:18 AM Rate Topic: -----

#1 MBraybrook  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 37
  • Joined: 14-August 06

PHP FTPS+cURL

Posted 22 April 2009 - 03:38 AM

I've searched all over the place for an answer, but come up short every time, so i'll post!

I'm trying to connect to a remote ftp server using cURL and ftps in PHP. The following is my code:

	curl_setopt($ch, CURLOPT_URL, 'ftp://$server/'.$localfile);
	curl_setopt($ch, CURLOPT_UPLOAD, 1);
	curl_setopt($ch, CURLOPT_INFILE, $fp);
	//SSL stuff
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
	curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
	curl_setopt($ch, CURLOPT_FTPSSLAUTH, 1);
	curl_setopt($ch, CURLOPT_PORT, $port);
	curl_setopt($ch, CURLOPT_USERPWD, $user:$pass);
	//curl_setopt($ch, CURL_SSLVERSION, 3);
	//end
	curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path . $localfile));
	curl_exec ($ch);


It may well be that some of those options cancel each other out, or aren't compatible, but i've been trying combinations of that for a while and none of them work.

If i use 'ftp://' for the CURLOPT_URL param then i get 'Access denied: 501'. Which labels itself curl error 67.
If i use 'ftps://' for the CURLOPT_URL param then i get 'error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol'. Which labels itself curl error 35.

Other notes:
I've not got direct access to the remote server, but i've been told that the logs show no attempt to connect whenever i have run this script.

The connection is required on port 61000, not a standard ftp(s) port.

Any guidance that anyone can offer would be greatly appreciated.

--
MBraybrook

Some other information that may be of use:
My cURL version is
libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
PHP was compiled with '--with-openssl'

And PHP logs show the following messages:
PHP Notice:  Use of undefined constant CURLOPT_FTP_SSL - assumed 'CURLOPT_FTP_SSL' in ...
PHP Notice:  Use of undefined constant CURLFTPSSL_TRY - assumed 'CURLFTPSSL_TRY' in ...
PHP Notice:  Use of undefined constant CURLOPT_FTPSSLAUTH - assumed 'CURLOPT_FTPSSLAUTH' in ...

This post has been edited by MBraybrook: 22 April 2009 - 03:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP FTPS+cURL

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: PHP FTPS+cURL

Posted 22 April 2009 - 01:36 PM

I'm wondering if there's a difference in your libcurl versions. I think I ran into that recently, as CURLOPT_FTP_SSL has been changed to CURLOPT_USE_SSL after 7.16.4, so you might try some substitutions. This page on libcurl's setopt function should be able to help a little.
Was This Post Helpful? 0
  • +
  • -

#3 MBraybrook  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 37
  • Joined: 14-August 06

Re: PHP FTPS+cURL

Posted 23 April 2009 - 11:13 AM

View PostJackOfAllTrades, on 22 Apr, 2009 - 12:36 PM, said:

I'm wondering if there's a difference in your libcurl versions. I think I ran into that recently, as CURLOPT_FTP_SSL has been changed to CURLOPT_USE_SSL after 7.16.4, so you might try some substitutions. This page on libcurl's setopt function should be able to help a little.


I've tried both permutations of the option texts and each returns a similar error. I'm going to take this to the hosting company and see what they can tell me about the setup.

Seems weird that phpinfo() shows that curl is installed and setup with SSL support, but that it doesn't recognise the constants.

M
Was This Post Helpful? 0
  • +
  • -

#4 MBraybrook  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 37
  • Joined: 14-August 06

Re: PHP FTPS+cURL

Posted 28 April 2009 - 04:44 PM

Ahah. Solved it.

It was a specific bug with PHP version 5.1.6 on a Fedora 5 box (not sure if it is specific to the OS, or a general php version error). See https://bugzilla.red...g.cgi?id=213987

It is fixed in 5.2 - so upgrading our php version fixed the issue. The final working script is as follows:
$ch = curl_init();
	$fp = fopen($localfile, 'r');
	curl_setopt($ch, CURLOPT_URL, $url . ':61000/' . $localfile);
	curl_setopt($ch, CURLOPT_UPLOAD, 1);
	curl_setopt($ch, CURLOPT_INFILE, $fp);
	//SSL stuff
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
	curl_setopt($ch, CURLOPT_FTP_SSL, CURLOPT_FTPSSLAUTH);
	curl_setopt($ch, CURLOPT_USERPWD, $usr:$pass);
	curl_setopt($ch, CURLOPT_SSLVERSION, 3);
	//end SSL
	curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
	curl_exec ($ch);
	$error_no = curl_errno($ch);
	$error_msg = curl_error($ch);
	curl_close ($ch);
	if ($error_no == 0) {
			$error = 'File uploaded succesfully.';
	} else {
			$error = 'File upload error.';
	}
	fclose($fp);


I hope this saves someone some time in the future.

Regards
Mark

This post has been edited by MBraybrook: 28 April 2009 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 cleverhelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-January 12

Re: PHP FTPS+cURL

Posted 10 January 2012 - 06:53 AM

The easiest way to do file operations on an ftps server is to use a wrapper. This website has a nice clean explaination: http://www.cleverit....rver-using-php/

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 gorkial  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-July 12

Re: PHP FTPS+cURL

Posted 05 July 2012 - 09:18 AM

Hi Guys..

This is other way to work with ftps and php...using curl


$today = date("Ymd");
//echo $today;
$ftp_server="ftps://myserver.com"; 
$ftp_user_name="someuser@myserver.com";
$ftp_user_pass="barrmandf3422";

$path="/FTP_DATA/SMSGT/";
$file =dirname(__FILE__)."/../application/archivosastras/".$localfile.".csv";
$remote_file = $path.$today.".csv";



$ch = curl_init();
$fp = fopen($file, 'r');


curl_setopt($ch, CURLOPT_URL, $ftp_server.$remote_file);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_PORT, 990);
//SSL stuff
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLOPT_FTPSSLAUTH);
curl_setopt($ch, CURLOPT_USERPWD, "$ftp_user_name:$ftp_user_pass");
//curl_setopt($ch, CURLOPT_SSLVERSION, 3);
//end SSL
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_exec ($ch);
$error_no = curl_errno($ch);
$error_msg = curl_error($ch);


echo "no".$error_no;
	curl_close ($ch);


	if ($error_no == 0) {
			$error = 'File uploaded succesfully.';
	} else {
			$error = 'File upload error.';
	}
	fclose($fp);



It works!!

This post has been edited by JackOfAllTrades: 05 July 2012 - 09:37 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1