【问题标题】:Can't load image in laravel 6无法在 laravel 6 中加载图像
【发布时间】:2020-05-11 11:59:30
【问题描述】:

我是 laravel 的新手

我刚刚成功上传了一张图片并在数据库中存储了名称。 也将“公共”文件夹链接到“存储/应用程序/公共”,但无法在页面上显示。 图像也在具有受保护随机名称的文件夹上。 我也无法使用该网址加载图像“http://localhost:8000/storage/flower.jpg”。 我现在该怎么办????

web.php

Auth::routes();
Route::post('/update/{id}','HomeController@update')->name('update');
Route::get('/home', 'HomeController@index')->name('home');

HomeContoller.php

public function update(Request $request,$id){

       $student = user::find($id);

       if ($file = $request->file('profile_image')) {
           $file = $request->file('profile_image');
           $request->file('profile_image')->store('public/');
           $img_nm = $file->getClientOriginalName();
           $request->file('profile_image')->getClientOriginalName();

           $student->image = $img_nm;
       }

         $student->username = $request ->username;
         $student->first_name = $request ->first_name;
         $student->last_name = $request ->last_name;
         $student->gender = $request ->gender;
         $student->email = $request ->email;
         $student -> save();
         return redirect()->route('home');

 }

home.blade.php

<td>
  <img src="{{ asset('storage/'.$student-> image) }}"/>
</td>

文件系统.php

'default' => env('FILESYSTEM_DRIVER', 'local'),

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

【问题讨论】:

  • 你能展示更多你的代码吗?
  • 先生,我已经更新了我的代码。请帮帮我。如果需要更多问我。谢谢
  • "将 'public' 文件夹链接到 'storage/App/public'" 你在使用符号链接吗?请注意,要使用链接 - 您的项目需要 Options Indexes FollowSymLinks &lt;Directory&gt; 在您的 apache 站点配置中。

标签: php laravel image file-upload laravel-blade


【解决方案1】:

你可以试试这个

<img src="storage/"{{$student-> image}}/>

【讨论】:

  • 先生,我尝试了更多类似的代码,也使用了你的代码。很抱歉没有成功。
【解决方案2】:

上传方式错误,如果需要原文件名则使用storeAs函数,

$file = $request-&gt;file('profile_image');

$img_nm = $file->getClientOriginalName();
$file->storeAs('images', $img_nm);
$student->image = $img_nm;

参考https://laravel.com/docs/6.x/filesystem#file-uploads

在你的刀片文件中,使用这个

<img src="{{asset('storage/images/'.$student->image)}}">

希望这会有所帮助。

【讨论】:

  • 先生,我用你的方法,但没有成功。我可以成功地将图像名称保存在数据库中。但无法在页面中显示。这是主要问题
猜你喜欢
  • 1970-01-01
  • 2010-12-28
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 2021-03-09
  • 2019-05-30
相关资源
最近更新 更多