0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Laravel】storageシンボリックのパスの対応のメモ

Posted at

アップロードした後のファイルをブラウザから参照するにはどういうパスのルールになるのか?
ということが整理したかったのでメモです。

階層

C:~\projectフォルダ\storage\app\public (ここまでLaravel標準階層?)
┗ asset (ここからユーザー任意のフォルダ階層)
┗ image
┗ 画像.jpeg

[シンボリックリンク生成先]

C:~\projectフォルダ\public\storage (ここまでLaravelでシンボリックリンクを生成するパス)
┗ asset (ここからユーザー任意のフォルダ階層)
┗ image
┗ 画像.jpeg

シンボリックリンクの「\public\storage」に実際のファイルパス「\storage\app\public」までが集約されていると理解しました。

ブラウザからの参照

以下のURLでブラウザから画像参照できるようになります。

localhost/project/public/storage/asset/image/画像.jpeg

「public/storage」まではLaravelの自動生成されたシンボリックリンクに従ったパス、
以降がユーザー任意のフォルダパス階層と同じです。

ファイルを保存するときの処理

[サーバー側で保存する処理(Repositoryなど)]

//実際に保存するパスを指定
//publicの前に自動で「storage\app\」が付くイメージ
$ICONFILE_DIR = 'public/asset\image/';
$path = $file->storeAs($ICONFILE_DIR,$fileName);

[ControllerでViewにパスの変数を渡す処理]

$CLIENT_ICONFILE_DIR = '/storage/asset/image/';
return view('/file/upload/complate', compact(['iconLink' => $CLIENT_ICONFILE_DIR, 'fileName' => '画像.jpeg']));

[View側]

<image src="{{ asset( $iconLink.$fileName) }}"></image>

その他

StorageやFileなどのファサードを使う場合、filesystem.phpで基準になるパスをカスタマイズできそうです。

[参考]
https://readouble.com/laravel/8.x/ja/filesystem.html
https://reffect.co.jp/laravel/laravel-storage-manipulation-master
https://codelikes.com/laravel-storage/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?