English 中文(简体)
How to SFTP upload files from PHP
原标题:

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

SFTP from PHP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?

I also tried phpseclib as suggested in:

SFTP from within PHP

But when i try phpseclib, i get these 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

I then tried using system commands in php like so, 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 terminal

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

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

All i can do right now is sftp from terminal and manually upload. But I really want to automate this so a PHP website can do all this.

There aren t many tutorials on how to SFTP upload from PHP. Is it because it s a bad thing to do? If so, what should I be doing? The server I want to upload to only allows sftp connections.

最佳回答

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

Per that, phpseclib s root directory needs to be in your include_path. From that page:

<?php
  set_include_path(get_include_path() . PATH_SEPARATOR .  phpseclib );

  include( Net/SFTP.php );
?>

You should really familiarize yourself with this kind of include technique - it s pretty standard for PEAR libraries and Zend ones.

问题回答

but my php server says ss2_connect is not defined.

For this error, you should install ssh2 extension on your server s php.

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

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

To use system command you have to:

  1. Configure password-less ssh
  2. Run command like scp filename1 userid@hostname:filename2, check man scp. You can do the same with sftp but with some other options

You can use Pure-PHP implementation of SSHv2 library.

Here are some examples of how to use this library:

<?php
include  vendor/autoload.php ;
$ssh = new phpseclibNetSSH2( www.domain.tld );
if (!$ssh->login( username ,  password )) {
    exit( Login Failed );
}
echo $ssh->exec( pwd );
echo $ssh->exec( ls -la );
?>

And:

<?php
include  vendor/autoload.php ;
$key = new phpseclibCryptRSA();
//$key->setPassword( whatever );
$key->loadKey(file_get_contents( privatekey ));
$ssh = new phpseclibNetSSH2( www.domain.tld );
if (!$ssh->login( username , $key)) {
    exit( Login Failed );
}
echo $ssh->read( username@username:~$ );
$ssh->write("ls -la
");
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签