【问题标题】:How to generate temporary file download url for laravel local storage? [closed]如何为laravel本地存储生成临时文件下载url? [关闭]
【发布时间】:2020-03-14 19:52:36
【问题描述】:

来自 Laravel 官方文档:

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

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

【问题讨论】:

    标签: php laravel temporary-files


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      相关资源
      最近更新 更多