How SFTP upload files with PHP

I'm having trouble using PHP to upload SFTP files to a remote server. When I use cURL, I get the error described here:

PHP SFTP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?

I also tried phpseclib as suggested in:

SFTP from within PHP

But when I try to use phpseclib, I get the following errors:

Warning: require_once(Math/BigInteger.php) [function.require-once]: failed to open stream: No such file or directory in /home/john/public_html/test/Net/SSH2.php on line 53 Fatal error: require_once() [function.require]: Failed opening required 'Math/BigInteger.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/john/public_html/test/Net/SSH2.php on line 53 

Then I tried using system commands in php, but nothing happened:

 <?php echo system('sftp user@ftp.domain.com '); echo system('password'); echo system('put file.csv'); ?> 

I also tried

 <?php $connection = ssh2_connect('shell.example.com', 22); ssh2_auth_password($connection, 'username', 'password'); ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644); ?> 

but my php server says ss2_connect is not defined.

I tried to do the following from the terminal

 scp file.csv user@ftp.remote.com password 

But the server does not allow the scp command. I do not have shell access to create ssh keys.

All I can do right now is sftp from the terminal and load manually. But I really want to automate this, so a PHP site can do it all.

There are not many manuals for downloading SFTP with PHP. Is it because it is bad? If so, what should I do? The server I want to load only allows sftp connections.

+4
source share
4 answers

http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_correct

Accordingly, the phpseclib root directory should be in your include_path. From this page:

 <?php set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib'); include('Net/SFTP.php'); ?> 

You really need to familiarize yourself with this inclusion technique - this is pretty standard for the PEAR and Zend libraries.

+6
source

but my php server says ss2_connect is not defined.

For this error, you must install the ssh2 extension on your php server.

+1
source

Use the built-in SSH library. Using the scp function will be easier than initiating an sftp session.

http://www.php.net/manual/en/function.ssh2-scp-send.php

0
source

To use the system command, you must:

  • Setting without ssh password
  • Run a command, for example scp filename1 userid@hostname :filename2 , check man scp . You can do the same with sftp , but with some other options
0
source

All Articles