首页 > 解决方案 > 当我尝试存储文件时,它不会存储在 storage\app\public

问题描述

我的问题是存储功能不会将歌曲存储在 storage/app/public 中,而是将其存储在 storage/app 中,因此我无法链接它,也无法播放歌曲。

我曾尝试在存储功能中编写公共目录,但是当我尝试播放歌曲时,他没有看到该文件,因为公共目录位于如下路径中:http: //127.0.0.1 :8000/storage/公开/the_song

控制器:

  public function store(Request $request)
  {
     $song = $request->the_song->store('songs');

     $music = Music::create([
        'sound'=> $song,
        'name'=> $request->name
     ]);

     session()->flash('success', 'The Song Successfully Saved');
     return redirect(route('music.index'));

   }

播放歌曲:

<audio controls autoplay>

  <source src="{{ asset($song->sound) }}" type="audio/ogg">
  <source src="{{ asset($song->sound) }}" type="audio/mpeg">

</audio>

标签: phphtmllaravel

解决方案


默认情况下,Laravel 使用local文件系统磁盘(请参阅参考资料config/filesystems.php)。要保存它,public您必须明确使用它:

$song = $request->the_song->store('songs', 'public');

并且在检索文件时:

Storage::disk('public')->url($song->sound)

或者您也可以将默认磁盘设置为public.


推荐阅读