我想存储只能由Laravel访问的文件(pdf、图像、视频)。这将迫使用户使用网站而不是url。
我如何才能做到这一点?有没有做到这一点的最佳实践?
谢谢!
下面是我的代码:
public static function downloadFile($id){
$file = FileManager::find($id);
//$file->location = ../app/storage/file/dam04/2-2/manual.pdf
//file->type = pdf
//file->name = instructor manual
$headers = array(
'Content-Type:'.mime_content_type($file->location),
);
return Response::download($file->location, $file->name.'.'.$file->type, $headers);
//exit;
}我不能把它烧起来。这些文件位于app/storage/中。
我真的需要一些建议来解释为什么这个方法行不通。谢谢,
发布于 2014-04-11 00:04:13
您可以使用Response::download()
创建路由器:
Route::get('/files/{fileName}', 'FileServerController@download');在你的控制器中,你可以做到
class FileServerController extends Controller {
public function download($fileName)
{
if (file_exists("$basepath/$fileName"))
{
return Response::download("$basepath/$fileName");
}
return Redirect::route('home')->withMessage('file not found');
// or return Response::make('File not found', 404);
}
}您可以对您的路由进行身份验证:
Route::get('/files/{fileName}', ['before' => 'auth', 'uses' => 'FileServerController@download']);而且,只要用户登录,就可以使用intended()开始下载文件:
if (Auth::attempt(...))
{
return Redirect::intended('home');
}https://stackoverflow.com/questions/22992833
复制相似问题