Download file from Slim Framework 2.4

主宰稳场 提交于 2019-12-06 08:12:39

Problem solved. Turned out to be a included php class with whitespace in it. This messed up the headers i guess.

Solved by creating a new, empty project and include step by step until the bad class showed.

Working solution for setting headers inside a Slim function;

<?php

require 'vendor/autoload.php';
$app = new \Slim\Slim();

$app->get('/foo', function () use ($app) {
    $app->response->headers->set('Content-Type', "application/pdf");
    $app->response->setBody("foo");
});

$app->run();
?>

Updated: This is the headers I use to let a user download a PDF:

$app->response->headers->set('Content-Type', $f->type);
$app->response->headers->set('Pragma', "public");
$app->response->headers->set('Content-disposition:', 'attachment; filename=' . $f->name);
$app->response->headers->set('Content-Transfer-Encoding', 'binary');
$app->response->headers->set('Content-Length', $f->size);
$app->response->setBody($f->data);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!