问题
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?
回答1:
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);
}
回答2:
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
will add phpseclib as a relative path. So doing include('Net/SFTP.php')
will include phpseclib/Net/SFTP.php
but that's relative to whatever your working directory is.
PEAR often has an absolute path in the include_path. So that's something you could try doing. Putting phpseclib in some directory (prob outside of the document root) and then putting that directory into your include_path.
Note that this matters more if you downloaded phpseclib from sourceforge.net or if you're on the 1.0 branch.
回答3:
Thanks to Diego, for the hint to install PEAR. I got it working with the following steps:
First, install PEAR.
$ wget http://pear.php.net/go-pear.phar
$ php go-pear.phar
If the wget command doesn't work, please install that package, to get it working.
$ sudo apt-get install wget
After successfully installing PEAR, configure it with this guide.
The php.ini
file, can be found in /etc/php5/apache2
. You have to modify this file.
Suddenly, I tested the new libary. But I got a bunch of error's instead, like in this stackoverflow post described. For that issue, I got the solution for it, in this stackoverflow post.
回答4:
I got it working by installing PEAR, following this guide.
By following the guide above, I figured out that apache2 was looking for php.ini
in my /etc/
folder. But I ran into a problem. In my /etc/
folder there was no php.ini
file. But there was php.ini.defualt
. So I 'saved as' php.ini.default
to php.ini
and then modified the include_path.
; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
include_path = ".:/Users/diego/pear/share/pear:/php/includes"
PHP is no longer throwing an error saying that phpseclib
cannot be found.
来源:https://stackoverflow.com/questions/30515751/how-to-install-phpseclib