Uploading Data via SFTP Using libssh2 (PHP 5)

Last week, I had a chance to work on transferring some data to the office server via SFTP , and one of the requirements was to put an excel file onto the server via SFTP. The steps below were done in Ubuntu 14.04 LTS .

Initial Research

I  started with doing google for php sftp . The first result shown was from the official PHP manual website (http://php.net/manual/en/function.ssh2-sftp.php). I clicked into the link and saw that PECL ssh2 is the main library used. I also noticed the two methods shown at the bottom of the page.

Immediately, I clicked intossh2_scp_send() and TADA, there were three lines of a simple code shown:

Frankly, the code satisfied me I would say, it was very simple, clean, and simple to use. I, then, did copy the code as usual into my IDE and peacefully tried to run it. Guess what!, the life was not too easy as we always think, yeh? I got the first error:

Fatal error: Call to undefined function ssh2_connect()

Installing PECL SSH2

That error was about missing  the SSH2 PECL package, so I managed to install it by using:

Restart the web server:

That’s the steps I found from http://php.net/manual/en/ssh2.installation.php
However, for me, it didn’t work, I needed to add one more line into php.ini:

at the last line (with nano, the shortcut for going to the last line is ctrl+v+w ) and then restart php5-fpm

That’s it, that error has already disappeared; still, I got a new error instead.

Final Code

After seeing the error, I was spending some time to find the cause, and it seemed to be that for some reasons, ssh2_scp_send()  won’t work in all server[1]. Hence,  as I saw many suggestions about using PHP’s fopen that already natively supports several protocols including SSH2[2], I decided to go for it, and this is my final code:

References:

  1. http://board.phpbuilder.com/showthread.php?10352595-SSH-SCP-functions-not-working-with-a-certain-server
  2. http://php.net/manual/en/wrappers.php

 

prapat
  • how about this, it much faster and simpler
    shell_exec(“sshpass -f key_file.keypass scp -P 1122 path_to/filename user@emzample.com:/var/www/tar_get_dir/”);

    • prapat

      Looks very simple indeed and should be faster Mr. Longka! I think I will try some time soon, but don’t you think using a native wrapper is more readable? Might have some slight difference though

  • The reason why I choose this approach is that it’s faster and its operation system library, it not depend on php5 or php7 😉

    • prapat

      That’s a good point. For a simple file transfer I would agree on that. Yet, I think for the more complicated task that needs several ssh functions better to use the built-in one, yeh? I see in PHP’s pages, there are lots of functionality provides, http://php.net/manual/en/book.ssh2.php.

      I also found another library too that a lot of people talking about too, http://phpseclib.sourceforge.net/.

  • Daniel Williams

    http://www.monstaftp.com offers SFTP/SCP as well as normal FTP through a web-based client that’s free to download. (disclaimer: I’m involved with this project.)