9

Slim 3 PHP フレームワークを使用してファイルをダウンロードしようとしています。Slim 3 もそうだと確信しているので、Slim 2 はかなり単純明快でしたが、私にはさっぱりわかりません。

どんな助けでも大歓迎です。ここのドキュメントに基づいて: http://www.slimframework.com/docs/objects/response.htmlここ からパッケージを追加しました: https://github.com/guzzle/psr7

したがって、この時点での私のコードは次のようになります。

$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);
});
4

2 に答える 2

11

私はphpからreadfileメソッドを使用しています:

$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;
}

この例は上記のリンクからのものです。これらのヘッダーを Slim Response オブジェクトと WithHeader メソッドで実装してみてください。

{filename}との違い[{filename}]は、2 番目の形式である filename がオプションであることです。

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

この例では、/my/route には引数が不要なのでアクセスできますが、/myother/route には not_optional 引数が必要なためアクセスできません。
詳細はSlim documentationにあります。

編集

$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;
});

このコードは私にとっては問題なく動作し、Slim 3.3.0 で作成されています

于 2016-03-14T20:58:10.177 に答える
7
$app->get('/test-download', function($request, Slim\Http\Response $response, $args) {
    $file = __DIR__ . '/2.csv';
    $fh = fopen($file, 'rb');

    $stream = new \Slim\Http\Stream($fh); // create a stream instance for the response body

    return $response->withHeader('Content-Type', 'application/force-download')
                    ->withHeader('Content-Type', 'application/octet-stream')
                    ->withHeader('Content-Type', 'application/download')
                    ->withHeader('Content-Description', 'File Transfer')
                    ->withHeader('Content-Transfer-Encoding', 'binary')
                    ->withHeader('Content-Disposition', 'attachment; filename="' . basename($file) . '"')
                    ->withHeader('Expires', '0')
                    ->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
                    ->withHeader('Pragma', 'public')
                    ->withBody($stream); // all stream contents will be sent to the response
});
于 2017-12-12T11:05:37.123 に答える