odan / slim4-skeleton

A Slim 4 Skeleton
https://odan.github.io/slim4-skeleton/
MIT License
439 stars 80 forks source link

Click to Download option #10

Closed 262925 closed 4 years ago

262925 commented 4 years ago

Is this possible to add a link to download with storage directory with slim 4 and how?

Ex. "Download" and Link is: (myweb/storage/04/download.zip).

odan commented 4 years ago

Could you please elaborate on that?

262925 commented 4 years ago

@odan Odan, A router link a page called /my-musics(twig tpl). In that page I had list of tracks which is loads from database. Below that a click to download all tracks as zip which is I stored in my slim app dir dts/storage/04/mymusic.zip - I wanted know give the link to download without showing my storage directory.

odan commented 4 years ago

You could read (or generate) a file from anywhere (internal filesystem, database etc) and then put content into the $response body. Here is an older article for Slim 3, but the same principle applies to Slim 4.

https://odan.github.io/2017/12/16/creating-and-downloading-excel-files-with-slim.html

262925 commented 4 years ago

I didn't yet tried above but i found following and working too. Download Zip Files Dynamically in PHP - Do you think which is good way on slim 4?

Code image

odan commented 4 years ago

Generally for Slim this is not a good way, because in Slim you should use the response object. For very large files you could also use streams (as written in my article).

262925 commented 4 years ago

I did followed. But i am getting error: 500 Internal Server Error - Error details: [0] Class 'Slim\Http\Stream' not found in C:\wamp\www\dts\src\Controllers\DownloadController.php on line 46.

Code: image

odan commented 4 years ago

Of course it must be a litte bit adapted to Slim 4, because the Stream class has been removed resp. abstracted away to other PSR-7 implementations like slim/psr7 etc. $stream = new \Slim\Psr7\Stream(...);

262925 commented 4 years ago

Cool.. works!