简体   繁体   English

Laravel下载未知类型的文件

[英]Laravel downloads a file with unknown type

I am using Laravel 4. I allow users to upload a file, which is programmatically renamed with some number and stored. 我正在使用Laravel 4.我允许用户上传一个文件,该文件以编程方式重命名并存储。 I also allow them to download the files, though I am supposed to rename their file from some funny number to their name, and download it. 我也允许他们下载文件,虽然我应该将他们的文件从一些有趣的数字重命名为他们的名字,然后下载它。

My problem is, how can I change filename just before its downloaded? 我的问题是,如何在下载之前更改文件名?

My code: 我的代码:

return Response::download($pathToFile, $name);

When I do that, the file is downloaded with unknown format. 当我这样做时,文件以未知格式下载。

The second $name parameter, needs to include the full filename (including the file extension). 第二个$name参数需要包含完整的文件名(包括文件扩展名)。 The download method does not automatically detect the extension from the file path and append it to the name, so that needs to be done manually. download方法不会自动检测文件路径中的扩展名并将其附加到名称,因此需要手动完成。 Something like this will work: 这样的东西会起作用:

return Response::download(
    $pathToFile,
    $name . '.' . pathinfo($pathToFile, PATHINFO_EXTENSION)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM