Php upload file using sftp
Personally, I prefer avoiding the PECL SSH2 extension. My preferred approach involves phpseclib, a pure PHP SFTP implementation. Here's an example with phpseclib 2.0 (requires composer): Show
Here's that same example with phpseclib 1.0:
One of the big things I like about phpseclib over the PECL extension is that it's portable. Maybe the PECL extension works on one version of Linux but not another. And on shared hosts it almost never works because it's hardly ever installed. phpseclib is also, surprisingly, faster. And if you need confirmation that the file uploaded you can use phpseclib's built-in logging as proof. Download and Extract the PHP library to the same directory level where the PHP script is to be used for connecting to a remote server via SFTP: https://sourceforge.net/projects/phpseclib Notes:
SFTP is a standard and secure protocol through which parties can safely transfer and share data and files. In any case, engaging with an SFTP server programatically can be challenging. In the following post, we’ll build a full fledged PHP based SFTP client, step by step, so that by the end of the guide, you’ll be able to do it all on your own! RequirementsFirst and foremost, you'll need an SFTP server to connect to. If you don't have one, you can get an SFTP endpoint on SFTP To Go in less than 30 seconds. PHP doesn’t come bundled with the SFTP libraries from the get-go, so we’ll need to install the required library ssh2 using PECL. The library relies on libssh2, so you must ensure that it is first installed on your machine. For example, on Ubuntu machines, run the following:
In accordance with the 12 factor app cloud development methodology, you should explicitly define your app’s dependencies, which would make our composer.json file look like this:
Now the real fun begins! Creating and dropping SFTP connectionsWe’ll begin by creating a new class that encapsulates the functionality required for our SFTP client. The public methods we first add allow us to connect to the server using either password authentication or key authentication, and provide the means to disconnect an existing session. connect-class.phpTo put our brand new methods to use, we’ll add a main function that instantiates our class and calls the connect-main.phpListing FilesNow that we have set up a successful connection, we can add a function to our class and use it to list files on the remote SFTP server. The listfiles.phpUpload FileNow it’s time to upload a file. Use the uploadfile.phpDownload FileLast
but not least: Use the downloadfile.phpSFTP To Go with maximum security and reliability SFTP To Go offers managed SFTP/FTPS/S3 as a service - maximum reliability, security, availability, with 1 minute setup. Great for companies of any size, any scale. Check out SFTP To Go!The Whole ThingThat’s it! If
you would like to run the entire program from start to finish, copy the following code and save it as main.phpTo run the code, use the command:
You can also clone our repo or check out more code samples on Github. |