laravel - 如何通过公共(public) URL 从存储中获取文件?

标签 laravel laravel-5

我有应用程序 Laravel。

路径下有一个存储:\storage\app\reports\

我尝试通过 GET url 获取文件,例如:

http://localhost:8000/storage/app/reports/report_27-05-19_05.xlsx

但这对我不起作用。

我还尝试在后端返回文件:

$file = File::get(storage_path($path.$filename));
$type = File::mimeType($path.$filename);

$response = Response::make($file, 200);
$response->header("Content-Type", $type);

return $response;

最佳答案

尝试这样的事情。

以下示例适用于存储在共享主机中的 csv 文件

$filename 是保存的文件的名称,将通过 get 请求传递

public function download($filename)
{
    $name = $filename;
    $file = $file = '/home/USERNAME/storage/report/'.$name;

    $header = array(
        'Content-Type' => 'application/csv',
        'Content-Disposition' => 'attachment',
        'Content-lenght' => filesize($file),
        'filename' => $name,
    );

    // auth code
    return Response::download($file, $name, $header);
}

关于laravel - 如何通过公共(public) URL 从存储中获取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56340029/

相关文章:

php - 如何使用 Laravel 查询生成器获取两个不同列的总和?

php - Laravel Eloquent Query 根据设定的条件查询多条记录

validation - 不能使用 boolean 验证 laravel 5

laravel phpunit 未知数据库':memory:

mysql - “PDOException”,消息为“SQLSTATE[HY000] [2002] 没有到主机的路由

php - Laravel 畸形字符 UTF-8

php - 覆盖created_at值

php - Laravel - 如何传递变量来重置密码模板?

php - 拉维尔 5.4 : Class 'App\Http\Controllers\Response' not found error

amazon-web-services - 没有 .env 文件的命令行生产中的 Laravel 和环境变量