如何在laravel中从网络浏览器访问上传的文件?

问题描述 投票:0回答:3

这就是我将文件上传到 laravel 项目中所做的事情。

if($request->file()) {
     $fileName = time().'_'.$request->file('imageFile')->getClientOriginalName();
     $filePath = $request->file('imageFile')->storeAs('products', $fileName, 'public');
     $product->featured_image = '/storage/' . $filePath;
 }

公盘

'public' => [
      'driver' => 'local',
      'root' => storage_path('app/public'),
      'url' => env('APP_URL').'/storage',
      'visibility' => 'public',
 ],

文件上传到 app/public/products 内的存储文件夹中,但当我尝试检索文件时显示 404 错误。即使我已经创建了与公共文件夹的符号链接,但不起作用。 我不知道为什么,但文件没有显示在公共文件夹中。文件仅上传项目级别或存储文件夹内的私人文件夹。我如何通过网络浏览器访问该文件。

php laravel file-upload laravel-storage
3个回答
1
投票

首先你创建了符号链接

php aritsan storage:link

这将在公共文件夹中创建快捷方式存储文件夹

这样你就可以访问网址

asset('storage/products/filename')

在您的情况下,您需要在存储时更改路径

  $product->featured_image =  $fileName;

所以在访问时

 asset('storage/products/'.$product->featured_image )

参考:https://laravel.com/docs/8.x/filesystem#the-public-disk


0
投票

根据文档 - “为了使这些文件可以从网络访问,您应该创建一个从 public/storage 到 storage/app/public 的符号链接”

php artisan storage:link

https://laravel.com/docs/8.x/filesystem


0
投票

在命令提示符下运行此命令对我有用:

rm public/storage
php artisan storage:link
© www.soinside.com 2019 - 2024. All rights reserved.