Asked  1 Year ago    Answers:  5   Viewed   19 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:

<?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.

 Answers

1

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.

Thursday, April 1, 2021
3

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);
Thursday, April 1, 2021
 
tpow
 
4

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();
    $api->expects($this->once())
        ->method('post')
        ->with('repos/l3l0/l3l0repo/git/blobs', $data)
        ->will($this->returnValue($expectedValue));

    $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
Saturday, May 29, 2021
4

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, May 29, 2021
2

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:

<?php
include('Net/SFTP.php');

$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!');
print_r($sftp->nlist());
?>
Saturday, May 29, 2021
 
Trott
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :