How to Send File over secure FTP SSL Protocol

14,684

Solution 1

The only way I've managed to do ftp over SSL using php was to use php's exec() function to execute a curl command. PHP's curl library would not work because at the time, the skip-pasv-ip option did not exist and it was something that was absolutely required. Something like:

curl --user <username:password> --disable-epsv --ftp-pasv --ftp-skip-pasv-ip --ftp-ssl --sslv2  --cert <path/to/certificate> -T <path/to/uploadfile> <hostname>

You may need to modify the curl options to suit your needs.

Solution 2

Perhaps you could use ftp_ssl_connect for that matter, which is used to open a secure SSL-FTP connection, and to upload a file is just a straight forward process, just create the connection to the server, and put the file up there. A basic example could be:

//Create your connection
$ftp_conn = ftp_ssl_connect( $host, $you_can_provide_a_port );

//Login
$login_result = ftp_login($ftp_conn, $user, $pass);

if( $login_result )
{
    //Set passive mode
    ftp_pasv( $ftp_conn, true );
    // Transfer file
    $transfer_result = ftp_put( $ftp_conn, $dest_file_path, $source_file_path, FTP_BINARY );

    //Verify if transfer was successfully made
    if( $transfer_result)
    {
        echo "Success";
    }
    else
    {
        echo "An error occured";
    }
}

For reference purposes http://www.php.net/manual/en/function.ftp-ssl-connect.php

Share:
14,684
JM4
Author by

JM4

I studied engineering/management of technology but got tossed into the world of web development after working years in a project management capacity. Work with healthcare/insurance companies to develop custom database and reporting solutions along with online enrollment applications.

Updated on June 05, 2022

Comments

  • JM4
    JM4 almost 2 years

    I appreciate any help that can be offered on the subject. At the end of an online enrollment, I am taking customer data (several fields), putting them in a CSV file and trying to submit to another client over SSL protocol but have no idea how this is done. I am also storing the information on a local database and am hoping the process is somewhat similar.

    I have already been sent links to view the SSH2 instructions from php.net SSN2

    but to be honest this is like reading Chinese to me. I do not understand the instructions and am not looking to install any extensions, modify the PHP.ini file or anything of the sort (especially since we do not own the server the information is being sent through).

    Is there a simple, secure way of transmitting this file to the SSL protocol provided to us?

    Thanks!

    • WhirlWind
      WhirlWind almost 14 years
      So, you are interested in using sftp or FTP-ssl?
    • JM4
      JM4 almost 14 years
      @WhirlWind - our client set up the following: Created secure FTP account and opened port 990 on the firewall for SSL connections on the FTP server. I also created a new certificate request for the FTP protocol, and choose 1028 bit SSL security Certificate.
    • WhirlWind
      WhirlWind almost 14 years
      Probably find some instructions for FTPS, instead of the SSH stuff, since that's what it sounds like the "client" wants.
    • JM4
      JM4 almost 14 years
      @WhirlWind - you think that's what I need to do - considering that is the topic of my question and what I asked above? Thanks for the insight
    • WhirlWind
      WhirlWind almost 14 years
      yes, I made that comment because you seem confused between SSH and SSL. They are two different things, and you say this is "Chinese to you." I didn't include it as an answer because it wasn't very complete. Good luck.
  • JM4
    JM4 almost 14 years
    thank you for the advice. I will give it a shot and let you know the results!
  • JM4
    JM4 almost 14 years
    Thanks for the advice. I am not familiar with Curl but i will give this a shot and let you know how it turns out!
  • Rob Apodaca
    Rob Apodaca almost 14 years
    @JM4 - Also wanted to point out that you might be able to use PHP's curl library. I wasn't able to use it because I could not set the ftp-skip-pasv-ip option - it wasn't available for the version of PHP I was using. If you don't need that option and have curl support available to you, give that a go first.
  • JM4
    JM4 almost 14 years
    this did not work as needed. I am given the client $host as IP address and it simply times out after 60 seconds. I know the FTPS works because I am able to connect with local FTP client
  • JM4
    JM4 almost 14 years
    I am not sure how to use curl inline as you have denoted above. I also referenced (curl.haxx.se/docs/manual.html) and they list inline just like you did. All of the CURL instructions I have seen are multiple lines with very specific options. I am trying to connect to a host IP, with given username and password, then dump a file in a specific directory. Any ideas?
  • Tim
    Tim over 12 years
    If the above doesn't work. And the connection does work via a desktop FTP program you probably have this problem. elitehosts.com/blog/…