2

I was wondering if I could get some help.

Im using the Flysystem package in my project.

At the moment, im uploading a file to S3 using the following code

$awss3 = new Flysystem(new AwsS3Adapter($client_details, 'bucket-name'));
$stream = fopen(public_path() . '/upload/' . $filename . '.gz', 'r+');
$awss3->writeStream($filename . '.gz', $stream);

This works perfect.

But i cant figure out how to run it in reverse and download the file.

Any help would be greatly appreciated.

Cheers,

BigJobbies
  • 3,633
  • 11
  • 43
  • 66

1 Answers1

2

You can use flysystem readStream function

$stream = $filesystem->readStream('something/is/here.ext');

Then you can just use write stream to write directly to disk

$filesystem->writeStream('backups/'.strftime('%G-%m-%d').'.backup', $stream)

Or you can download the file directly using below snippet

            //Get File Extension
            $file_extension = ;

            //Get File Size
            $file_size = ;

            // Set the headers, prevent caching.
            header("Pragma: public");
            header("Expires: -1");
            header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");

            header("Content-Transfer-Encoding: binary");
            header("Content-Type:application/" . $file_extension);
            header('Content-Disposition: attachment; filename="' . $file_name . '"');

            header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");

            header('Content-Length: ' . $file_size);
            header('Accept-Ranges: bytes');
            fpassthru($stream);