猿问

如何为laravel本地存储生成临时文件下载url?

来自 Laravel 官方文档:

对于使用 s3 或 rackspace 驱动程序存储的文件,您可以使用temporaryUrl 方法为给定文件创建一个临时URL。

有没有办法让文件临时下载 url 而不是使用 s3/etc 和仅用于本地存储。我正在使用https://github.com/spatie/laravel-medialibrary库进行开发。


湖上湖
浏览 351回答 1
1回答

繁花不似锦

public function downloadFileAction(){    if (isset($_REQUEST['file_name'])) {        $pathFile = STORAGE_PATH.'/'.$_REQUEST['file_name'];        header('Content-Description: File Transfer');        header('Content-Disposition: attachment; filename="'.$_REQUEST['file_name'].'"');        header('Content-Type: application/octet-stream');        header('Content-Transfer-Encoding: binary');        header('Content-Length: ' . filesize($pathFile));        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');        header('Pragma: public');        header('Expires: 0');        readfile($pathFile);    }}public function uploadFile(array $file)    {        $storagePath = STORAGE_PATH;        @mkdir($storagePath, 0777, true);        $fullPath = $storagePath. uniqid (time()). $file['name'];        $fileTemp = $file['tmp_name'];        move_uploaded_file($fileTemp, $fullPath);        return $fullPath;    }在您的控制器中,您可以使用以下文件获取文件:$_FILES
随时随地看视频慕课网APP
我要回答