【问题标题】:Cannot download file from storage folder in laravel 5.4无法从 laravel 5.4 中的存储文件夹下载文件
【发布时间】:2017-09-05 01:34:39
【问题描述】:

我已通过$path=$request->file->store('files') 将文件存储在 storage/app/files 文件夹中,并将路径“files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc”保存在表的列名file中。

我还通过php artisan storage:link将存储文件夹链接到公共。

在我的视图刀片文件中,我把这个

<a href="@if(count($personal_information)) {{asset('storage/'.$personal_information->file)}} @endif" download>Download File</a>

下载文件的链接是http://localhost:8000/storage/files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc

但我得到了错误

RouteCollection.php 第 161 行中的 NotFoundHttpException

如果我在/storage 之后添加/app,它会给出同样的错误。如何从我的storage/app/files 文件夹下载文件?

【问题讨论】:

标签: laravel-5


【解决方案1】:

对于 Laravel 8

上传文件到存储文件夹

在本例中,我在存储文件夹中创建了一个测试文件夹名称:uploadfiles

FileSystem.php

'uploadedfiles' => [
   'driver' => 'local',
   'root' => storage_path('uploadedfiles'),
],

上传文件

public function upload_file()
{
  $file_name = "files_"."_".strtotime("now")."_".$_FILES['file']['name'];
  $content = file_get_contents($_FILES['file']['tmp_name']);
  Storage::disk('uploadedfiles')->put($file_name,$content);
}

从存储文件夹下载文件

Route::get('download/files/{filename}', function($filename) {
  $file = Storage::disk('uploadedfiles')->download($filename);
  return $file;
});

【讨论】:

    【解决方案2】:

    Laravel8

    创建链接

    <a href="{{ url('download?path='. $user->avatar) }}">
        Download
    </a>
    

    定义路线

    Route::get('download', [DownloadController::class,'download']);
    

    处理它

    public function download(Request $request){
        return Storage::download($request->filePath);
    }
    

    【讨论】:

      【解决方案3】:

      如果您的文件存储在公共目录中,请使用:public_path()。但如果您的文件存储在 storage 目录中,则使用:storage_path()

      注意:如果您的存储文件夹仅包含附件文件夹,请考虑使用这种方式:storage_path('photos/');

      $destination = storage_path('storage/photos/'); 
                     or
      $destination = public_path('storage/photos/');
      $filename = "user_3423423465.png";
      $pathToFile = $destination.$filename;
      return response()->download($pathToFile,'user_profile_photo.png');
      

      【讨论】:

        【解决方案4】:

        我正在使用 Laravel 6.X 并且遇到了类似的问题。根据您的问题,解决方法如下: 1)在你的 routes/web.php 做类似的事情

        /**sample route**/
        Route::get('/download/{path}/', 'MyController@get_file');
        

        2)然后在您的控制器(MyController.php)中,对于我们的案例,它应该如下所示:

        
            <?php
            
            namespace App\Http\Controllers;
            
            use App\Http\Controllers\Controller;
            
            class MyController extends Controller
            {
                public function get_file($path)
                {
                    /**this will force download your file**/
                    return response()->download($path);
                }
            }
        
            
        

        【讨论】:

          【解决方案5】:

          问题是 storage 文件夹默认情况下不可公开访问。存储文件夹最有可能保存一些私人文件,例如其他用户无法访问的用户图片。如果您将它们移动到公用文件夹文件将可供所有人访问。我在 Laravel 5.4 中遇到了类似的问题,我通过编写一个下载文件的路径做了一个小问题。

          Route::get('files/{file_name}', function($file_name = null)
          {
              $path = storage_path().'/'.'app'.'/files/'.$file_name;
              if (file_exists($path)) {
                  return Response::download($path);
              }
          });
          

          或者您可以将文件保存到 public 文件夹中,由您自己决定。

          【讨论】:

          • Response:: 来自哪里?
          猜你喜欢
          • 1970-01-01
          • 2021-08-26
          • 2018-11-26
          • 1970-01-01
          • 2018-09-10
          • 1970-01-01
          • 2015-05-20
          • 1970-01-01
          • 2017-12-10
          相关资源
          最近更新 更多