Viewed   152 times

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:

echo system('sftp');
echo system('password');
echo system('put file.csv');

I also tried

$connection = ssh2_connect('', 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

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.



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

  set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');


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

Friday, November 4, 2022

If you would read through the documentation, you would find out that the second argument of the put() function is $data, therefore not the file path, but the actual data to write:

function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1)

By default, NetSFTP::put() does not read from the local filesystem. $data is dumped directly into $remotefile. [...]

To upload a local file, the easiest way is to read the content into one variable that will be passed to the put() function:

$data = file_get_contents("logo.png");
$sftp->put("/some/path/logo.png", $data);

Edit: You are probably using a new version of phpseclib, which renamed these constants to make them more object-like. With a new version, you should use

$sftp->put("/some/path/logo.png", "logo.png", SFTP::SOURCE_LOCAL_FILE);
Sunday, November 20, 2022

As mentioned in "GitHub API - write to repo", you would need to create a blob.

You can see an example in KnpLabs/php-github-api/test/Github/Tests/Api/GitData/BlobsTest.php#L53-L68

(I am using KnpLabs/php-github-api, since ornicar/php-github-api is deprecated and refers to it)

 * @test
public function shouldCreateBlob()
    $expectedValue = array('blob' => 'some data');
    $data = array('content' => 'some cotent', 'encoding' => 'utf8');

    $api = $this->getApiMock();
        ->with('repos/l3l0/l3l0repo/git/blobs', $data)

    $this->assertEquals($expectedValue, $api->create('l3l0', 'l3l0repo', $data));

There is no filename yet: you upload content;

To create a file though, as shown in this example, you would still need to:

  • get the SHA the current master branch points to
  • fetch the tree this SHA belongs to
  • create a new tree object with the new blob, based on the old tree
  • create a new commit object using the new tree and point its parent to the current master
  • finally update the heads/master reference to point to the new commit
Friday, October 28, 2022

Ok I got it finally, I just had to do this:-

$myclass=new java("histvolone.histvol");

instead of

$myclass=new java("histvol");

It worked !

Saturday, September 24, 2022

I don't think your put is doing what you think it is doing. According to the docs, you need to do a Net_SFTP::chdir('/some-dir/') to switch to the directory you want to send file to, then put($remote_file, $data), where remote_file is the name of file, and $data is the actual file data.

Example Code:


$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');

echo $sftp->pwd() . "rn";
$sftp->put('filename.ext', 'hello, world!');
Saturday, September 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :