PHP - SSH2 SFTP Downloads Successfully

I feel this deserves a question since StackOverflow seems to be missing out on this topic.

I want to use SFTP using PHP with a progress indicator

I like the progress indicators . However, this time I want to do something completely different. I want to do the following with PHP, and it looks a lot more complicated than the last:

  • Download via SFTP (via SSH?)
  • Give the user a visible indication of the loading process.
  • Enable pause and continue downloading

Think FileZilla , but in a browser built with PHP. I do not want to hack Apache or add any Apache motifs.


Options for actual download

There are many questions here , here and here for the basic SFTP download.

This document is a PHP SSH2 extension (which I am currently using - you install it via pecl) and an alternative PHPSecLib , which I do not use, but may take a look later.
My interface makes it easy to switch I / O - the coding of the interface, not the implementation, etc.

This is great, but they just do the actual download and what it is.

Boot process

PHP has a really interesting stream_notification_callback , which you can read more about here .

This looks great and was a promising step until someone studied the PHP source code and found that, unfortunately, SSH2 / SFTP does not allow integration with this

Thanks to hek2mgl for learning this.

The idea with stream_notification_callback was to send a notification of the current download size each time data is retrieved; therefore giving the data needed to calculate the percentage using the current loaded amount and the total file size. But this does not happen with SSH2 and SFTP ...

What about pausing / continuing?

In my opinion, this would be the most difficult to accomplish. Loading data into a temporary file would be possible ... Here's what I managed to dig: http://ee.php.net/manual/en/function.fread.php#84115 - But integrating such code with a progress indicator seems crazy.

Finally

There is also cURL, however I have not seen pausing / resuming downloads as much as possible using SFTP. Correct me if I am wrong.

So, how do I go about integrating the above requirements in a browser using PHP? Forget about the client side, just get the data in the browser, so the recommendations for doing this will be great.

+7
source share
2 answers

Here is a sample that I took from this site :

  // Write from our remote stream to our local stream $read = 0; $fileSize = filesize("ssh2.sftp://$sftp/$fileName"); while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) { // Increase our bytes read $read += strlen($buffer); // Write to our local file if (fwrite($localStream, $buffer) === FALSE) { throw new Exception("Unable to write to local file: /localdir/$fileName"); } } 

Since you have the file size and you read it with fread, it is trivial to determine your progress.

Pause can be done by checking the value, which allows you to specify the APC that you set if the user clicks the "Pause" button (this is done in the iframe, so you do not stop the main script).

+2
source

Check Server Fails When Uploading Large Files Using PHP . I can give you some ideas.

To summarize ... one of the answers suggests modifying phpseclib to echo data every time a piece is received. Another answer suggests simply loading into chunks and outputting chunks as they load.

It looks like you enjoy using stream_notification_callback.

Good luck

+1
source

All Articles