【问题标题】:Content-Type: image/jpeg not working on live working on local内容类型:图像/jpeg 无法在本地实时工作
【发布时间】:2016-08-22 18:33:44
【问题描述】:

我对超薄框架非常陌生,我正在尝试下载图像文件。使用 php 在本地工作正常。但是当在服务器上应用相同的代码时,它不起作用。搜索了很多但没有运气,即herethis one。这是我的代码

$app->get('/image', function ($request, $response, $args) {
//header('Content-type: image/png');
//header('Content-Disposition: attachment; filename=go-logo.jpg');
//$contents = file_get_contents(getcwd() . '/' . $_GET['img']);
$filename=getcwd() . '/' . $_GET['img'];
header("Content-Length: " . filesize($filename));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=go-logo.jpg');
readfile($filename);
return $contents;

});

错误是它显示它是一个 html 文件。我想要这样的

【问题讨论】:

  • 不工作是什么意思? $contents 也没有在任何地方定义。
  • 对不起。我忘了附上屏幕短片
  • 请立即查看
  • 您是否下载了图像并使用编辑器打开?这可能是服务器给出的错误页面。

标签: php image content-type slim


【解决方案1】:

您需要在响应对象上设置标头。 然后将文件写入响应体。

return $response->withHeader("Content-Type", "application/octet-stream")
        ->write($fileContents);

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 2017-12-11
    • 2013-08-21
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多