How to Sftp Upload Files from PHP

Uploading files with SFTP

With the method above (involving sftp) you can use stream_copy_to_stream:

$resFile = fopen("ssh2.sftp://{$resSFTP}/".$csv_filename, 'w');
$srcFile = fopen("/home/myusername/".$csv_filename, 'r');
$writtenBytes = stream_copy_to_stream($srcFile, $resFile);
fclose($resFile);
fclose($srcFile);

You can also try using ssh2_scp_send

How to SFTP upload files from PHP

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.

How to transfer a file to the server using sftp?

If you just want to transmit a file and don't care about using scp or sftp, then I would encourage you to use scp. Here comes a basic example (taken from the PHP manual):

$connection = ssh2_connect('www.example.com', 22);
if($connection === FALSE) {
die('Failed to connect');
}

$state = ssh2_auth_password($connection, 'username', 'password');
if($state === FALSE) {
die('Failed to authenticate');
}

$state = ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
if($state === FALSE) {
die('Failed to transfer the file');
}

PHP SFTP Simple File Upload

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() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>

SFTP local file upload using PHP

Which library is NET/ssh2.php? Looks like http://phpseclib.sourceforge.net?

You're mixing their SSH2 and SFTP libraries. Taken directly from their manual, the code you want is:

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

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

echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>

So just change the $data in the "put" function to be the filename, and adding the mode as you've done in your code.

An alternative would be the PECL functions provided by PHP. These have sftp functions "built in". Example code is http://www.php.net/manual/en/function.ssh2-sftp.php and if it still doesn't work, we'll be better placed to help debug as we can all access it.

How to upload image from HTML form using SFTP?

According to the documentation the order of your arguments should be the other way round.

$sftp->put('/home/new_dir/'.$uploaded_file, $uploaded_file, NET_SFTP_LOCAL_FILE);

Upload file with sftp and php

PHP is a server side scripting language. When your browser requests a PHP generated site, PHP runs on the server and your browser only gets to see the result. For that reason, PHP obviously has no way to access client side files: it already ran before anything even reaches the client computer.

I'm assuming the server with the PHP files on it and the SFTP server are different servers, otherwise your whole question doesn't make too much sense.

So, what you need to do here is a two step approach: First, you need to upload the files to the server who runs the PHP files the regular way, using a HTTP POST request. You can send the request to a PHP script, that then uses SFTP to move the files to the other server.


For downloads (as you asked this in your comments) it works similar: the browser requests a PHP script that fetches the file from the SFTP server, and then sends it to the browser as HTTP response. The browser should then display a regular file download dialog and the user can select where to store it.


FOr uploading you should consider using some kind of cron job or a background job started using PHP's exec() instead, as you will most likely either run into max execution timeouts or have to set them way higher than you should if you upload them usign PHP, especially for large files. The alternative is to use a separate PHP configuration (depending on what version of PHP you are running you can use .htaccess files, .user.ini files or different PHP-FPM pools for that) to increase the execution time for only the upload script.

Upload file from local to SFTP via PHP SSH2

In FileZilla, you upload a file under a name diamondexclusive.mp4 to the current remote working directory, which is /home/myfarewellnote/web.

Hence the full target path is /home/myfarewellnote/web/diamondexclusive.mp4.

While in PHP you upload the file to /Users/petenaylor/Desktop/diamondexclusive.mp4 (what is actually a local source path, that has nothing to do with the server).

Use the same path that you upload the file to in FileZilla:

file_put_contents("ssh2.sftp://{$sftp}/home/myfarewellnote/web/diamondexclusive.mp4", $contents);

How to increase the performance of a file upload using native sftp functions and fwrite in PHP

The issue is that even though 32MB are read and then written to the sftp stream, fwrite will chunk at a different size. I think just a few KB.

For filesystems (which is the usual case with fwrite) this is fine, but not with high latency due to fwriting to a remote server.

So the solution is to increase the chunk size of the sftp stream with

stream_set_chunk_size($stream, 1024 * 1024);

So the final working code is:

<?php

$connection = ssh2_connect($this->host, $this->port, null);
$sftp = ssh2_sftp($connection);

$connection_string = ((int) $sftp) . $remotePath . $remoteFilename;
$stream = fopen('ssh2.sftp://' . $connection_string, 'w');
$source = fopen($localFilepath, 'r');

// Stream chunk size 1 MB
stream_set_chunk_size($stream, 1024 * 1024);

if (!$stream) {
throw new Exception('Could not create file: ' . $connection_string);
}

while (!feof($source)) {
// Chunk size 32 MB
if (fwrite($stream, fread($source, 33554432)) === false) {
throw new Exception('Could not send data: ' . $connection_string);
}
}

fclose($source);
fclose($stream);

Hope this helps the next person that is getting gray hair trying to figure that out ;)



Related Topics



Leave a reply



Submit