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

时间:2020-03-14 19:52:36

标签: php laravel temporary-files

来自Laravel官方文档:

对于使用s3或机架空间驱动程序存储的文件,您可以创建一个 使用临时Url方法指向给定文件的临时URL。

有什么方法可以为文件提供临时下载URL,而不是使用s3 / etc和仅用于本地存储。我正在使用https://github.com/spatie/laravel-medialibrary库进行开发。

1 个答案:

答案 0 :(得分:3)

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