我希望能够点击一个链接,它会从 Laravel 公用文件夹下载一个文件。我需要查看为实现此目的而需要编写的路由和 Controller 。
我可以只通过 php 实现吗?
if (isset($_GET["download"])){
$search = $_GET["download"];
$dir = $clipDir->clip_path1($search);
$Base = basename($dir);
if (file_exists($dir)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$Base);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($dir));
ob_clean();
flush();
readfile($dir);
exit;
} else {
echo 'File Dont Exit <br />'. print_r($dir);
return false;
}
文件存储在 Laravels 公用文件夹中。
最佳答案
看看Response::download并做类似的事情:
return Response::download($yourfile, 'nameoffile.extension');
关于php - 使用 laravel 强制链接下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35571817/