資料
laravel學院講解 | |
---|---|
鏈接 |
文件存儲配置(具體配置可以看官網講解這里我們使用默認):
文件系統的配置文件位於 config/filesystems.php 。
在這個文件中你可以配置所有「磁盤」。
每個磁盤代表特定的存儲驅動及存儲位置。
每種支持的驅動程序的示例配置都包含在配置文件中。

公共磁盤鏈接
你可以使用`Artisan` 命令`storage:link`來創建符號鏈接:
php artisan storage:link
2.使用路由
Route::get('save','SaveController@put');
Route::get('get','SaveController@get');
Route::get('delete','SaveController@delete');
Route::get('download','SaveController@download');
2.1use類:
use Illuminate\Support\Facades\Storage;
2.3簡單的在控制器中的使用:
public function put()
{
Storage::put('test.txt', '我是測試文字');#保存.txt文件,參數2,保存文字內容
}
public function delete()
{
Storage::delete('test.txt');#刪除已經保存的test.txt文件
}
public function get()
{
return Storage::get('test.txt');#得到test.txt的內容
}
public function download(){
return Storage::download('test.txt');#下載test.txt文件
}
下面來看一下實際中的使用過程:
public function upload(Request $request){
if ($request->isMethod('post')){
$img=$request->file('file');
//獲取圖片的擴展名
$name=$img->getClientOriginalExtension();
//獲取文件的絕對路徑
$path=$img->getRealPath();
//定義新的文件名
$filename=date('Y-m-d-h-i-s').'.'.$name;
Storage::disk('public')->put($filename,file_get_contents($path));
}
}
自定義文件上傳驅動位置
在
config/filesystems.php
下的disks
中自定義

使用
Route::post('uploads_file', function () {
#實現自定義文件上傳
$file = request()->file('file');
//獲取文件的擴展名
$name = $file->getClientOriginalExtension();
//獲取文件的絕對路徑
$path = $file->getRealPath();
//定義新的文件名
$filename = date('Y-m-d-h-i-s') . '.' . $name;
dd(Storage::disk('file')->put($filename, file_get_contents($path)));
});
效果展示

自定義下載驅動
我們需要語義化設置存儲位置的時候
在config\filesystems.php
disks中定義 excel驅動
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
'excel' => [
'driver' => 'local',#本地驅動
'root' => storage_path('excel/download'),# 保存的位置
'url' => env('APP_URL').'/storage',
'visibility' => 'public',#可見性
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
],
],
- 定義對應如下
- 使用:下載excel文件
public function getExcelImport()
{
return Storage::disk('excel')->download('用戶管理池導出模板.xlsx');
}