I have installed Apache on my machine so I can use localhost as a PHP server. That works. Right now I am trying to use PHP to send files via SFTP to another server. I looked around a bit and saw phpseclib was recommended. I can't seem to figure out how to install phpseclib. Here are the lines that the website gave:
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SFTP.php');
You have to set the include path.
I understand what these words mean but I have been unable to get it working. I am using a mac. My server is in /Users/diego/Sites/
. Where exactly do I need to put the phpseclib
folder? What lines do I need to put in my php file so that phpseclib
is included?
I tried installing phpseclib on linux machine. It worked perfectly for me. I hope you need a similar configuration on your Mac.
On linux server execute following command.
sudo apt-get install php5-pgsql php-pear
sudo pear channel-discover phpseclib.sourceforge.net
sudo pear remote-list -c phpseclib
sudo pear install phpseclib/Net_SSH2
sudo service apache2 restart
On Mac, using mac port execute following command sudo port -v selfupdate sudo port install php54-ssh2
#add this to the php.ini file /etc/php.ini, it can be placed at the end of the file
extension=/opt/local/lib/php54/extensions/no-debug-non-zts-20100525/ssh2.so
sudo /usr/sbin/apachectl restart
On Mac, using homebrew execute following command. brew install php54-ssh2
Download phpseclib library and include into your project directory.
In the relevant php file add following code.
include('phpseclib1.0.5/Net/SFTP.php');
set_include_path(get_include_path().PATH_SEPARATOR.'phpseclib');
define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
$sftp = new Net_SFTP('555.55.55.55', 22);
if(!$sftp->login('username', 'password')) {
echo $sftp->getSFTPLog();
die('Login failed!');
} else {
echo $sftp->pwd();
echo $upload = $sftp->put('/sftp/'.$filename, $uploadedfile, NET_SFTP_LOCAL_FILE);
}