Slim 3 Framework how to download file

Jim_M picture Jim_M · Mar 14, 2016 · Viewed 15k times · Source

I'm trying to download a file using the Slim 3 PHP Framework. Slim 2 was fairly straight forward, as I'm sure Slim 3 is too, but I just don't get it.

Any help would be appreciated. Based on the documentation here : http://www.slimframework.com/docs/objects/response.html I've added the package from here : https://github.com/guzzle/psr7

So my code at this point looks like :

$app->get('/worksheet/download/{filename}', function ($request, $response, $args) {

    error_log("__________________________");

    $fileName = $args['filename'];
    error_log($fileName);
    $newStream = new \GuzzleHttp\Psr7\LazyOpenStream($fileName, 'r');


    $newResponse = $response->withHeader('Content-type', 'application/octet-stream')
        ->withHeader('Content-Description', 'File Transfer')
        ->withHeader('Content-Disposition', 'attachment; filename=' . basename($fileName))
        ->withHeader('Content-Transfer-Encoding', 'binary')
        ->withHeader('Expires', '0')
        ->withHeader('Cache-Control', 'must-revalidate')
        ->withHeader('Pragma', 'public')
        ->withHeader('Content-Length', filesize($fileName))
        ->withBody($newStream);
return($newResponse);
});

Answer

legomolina picture legomolina · Mar 14, 2016

I'm using readfile method from php:

$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment;filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}

This example is from the link above, maybe you can try to implement theses headers with the Slim Response object and WithHeader method.

The diference between {filename} and [{filename}] is that second form, filename is optional.

$app->get('/my[/{optional}]/route', function($req, $res, $args) {});
$app->get('/myother/{not_optional}/route', function($req, $res, $args) {});

In this example, you can access to /my/route because argument is not required but you can't access to /myother/route because you NEED not_optional argument.
There are more info in Slim documentation.

EDIT

$app->get('/download', function($req, $res, $args) {
    $file = 'public_html/images/back2.jpg';
    $response = $res->withHeader('Content-Description', 'File Transfer')
   ->withHeader('Content-Type', 'application/octet-stream')
   ->withHeader('Content-Disposition', 'attachment;filename="'.basename($file).'"')
   ->withHeader('Expires', '0')
   ->withHeader('Cache-Control', 'must-revalidate')
   ->withHeader('Pragma', 'public')
   ->withHeader('Content-Length', filesize($file));

readfile($file);
return $response;
});

This code works fine for me and it's made with Slim 3.3.0