【问题标题】:How do I force download of a file through Flysystem?如何通过 Flysystem 强制下载文件?
【发布时间】:2016-07-20 20:58:24
【问题描述】:

我可能在这里遗漏了一些特别明显的东西,但我正在使用 yii2-flysystem 和 Dropbox 来读取和写入文件。

我可以毫无问题地将它们上传并写入 Dropbox,但是当这样阅读时:

$file = Yii::$app->dropboxFs->read($fn);

..所有给我的都是一个字符串 (/tmp/phpQkg8mJ)。

我实际上如何强制下载我正在阅读的文件?我不确定该临时文件位置实际上与什么相关。

【问题讨论】:

标签: php yii2 flysystem


【解决方案1】:

尝试函数readfile()。 根据示例,您的代码应如下所示:

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

【讨论】:

  • 不幸的是 file_exists($file) 返回 false...如果我执行 readfile($file) 我得到“没有这样的文件或目录”
猜你喜欢
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 2012-09-19
  • 1970-01-01
相关资源
最近更新 更多