在config下新建文件admin.php,定義上傳文件的路徑
'upload_img_path' =>'app/public/img',//本地上傳圖片路徑
'upload_file_path' =>'app/public/files'//本地上傳文件路徑
在config/filesystems.php下定義
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
'disks' => [ 'uploadimg' =>[ 'driver' => 'local' , 'root' =>storage_path(config( 'admin.upload_img_path' )) ], 'uploadfiles' =>[ 'driver' => 'local' , 'root' =>storage_path(config( 'admin.upload_file_path' )) ], 'local' => [ 'driver' => 'local' , 'root' => storage_path( 'app' ), ], 'public' => [ 'driver' => 'local' , 'root' => storage_path( 'app/public' ), 'url' => env( 'APP_URL' ). '/storage' , 'visibility' => 'public' , ], 's3' => [ 'driver' => 's3' , 'key' => env( 'AWS_KEY' ), 'secret' => env( 'AWS_SECRET' ), 'region' => env( 'AWS_REGION' ), 'bucket' => env( 'AWS_BUCKET' ), ], ], |
后臺上傳方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public function uploadImg(Request $request ){ $wenjian = $request ->file( 'files' ); if ( $wenjian ) { //獲取文件的原文件名 包括擴展名 $yuanname = $wenjian ->getClientOriginalName(); //獲取文件的擴展名 $kuoname = $wenjian ->getClientOriginalExtension(); //獲取文件的類型 $type = $wenjian ->getClientMimeType(); //獲取文件的絕對路徑,但是獲取到的在本地不能打開 $path = $wenjian ->getRealPath(); //要保存的文件名 時間+擴展名 $filename = date ( 'Y-m-d' ) . '/' . uniqid() . '.' . $kuoname ; //保存文件 配置文件存放文件的名字 ,文件名,路徑 $bool = Storage::disk( 'uploadimg' )->put( $filename , file_get_contents ( $path )); //return back(); return json_encode([ 'status' =>1, 'filepath' => $filename ]); } else { $idCardFrontImg = '' ; return json_encode( $idCardFrontImg ); } } |
前臺顯示
1
|
<img src= "/storage/img/2018-04-27/5ae294e2830df.jpeg" > |
在寫接口上傳的照片如何保存到public讓前端框架訪問到,,就要建立軟連接將照片放到public目錄去訪問! 很簡單
執行命令:php artisan storage:link
命令執行完畢后,就會在項目里多出一個 public/storage,
這個 storage 就是一個軟鏈接,它指向 storage/app/public 目錄。
public/storage(軟連接) → storage/app/public
然后就可以用地址直接訪問public里面的照片了!
以上這篇laravel 實現上傳圖片到本地和前臺訪問示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/Tony_110/article/details/80105099