Laravel-文件上傳
標簽(空格分隔): php
介紹
Laravel 基於 Frank de Jonge 開發的 PHP 包 Flysystem 提供了強大的文件系統抽象。Laravel 文件系統集成對使用驅動處理本地文件系統進行了簡化,這些驅動包括Amazon S3,以及 Rackspace 雲存儲。此外在這些存儲選項間切換非常簡單,因為對不同系統而言,API 是一致的。
在web應用中,最常見的存儲文件案例就是存儲用戶上傳的文件,如用戶頭像、照片和文檔等。Laravel通過使用上傳文件實例上的store方法讓存儲上傳文件變得簡單。你只需要傳入上傳文件保存的路徑並調用store方法即可
配置
修改config/filesystems.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "s3", "rackspace"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
|-----------------------------------------------------------------------------------|
# 增加配置 [路徑可以自定義 storage_path('app/upload') 對應的是 /storage/app/upload ]
# 如果想修改的話 public_path('/uploads') 對應的是 /public下的uploads
'upload' => [
'driver' => 'local',
'root' => storage_path('app/upload'),
],
|-----------------------------------------------------------------------------------|
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
];
實現
# 第一種方式
$fileName = $request->file('image')->store('upload');
file('image') => form表單的name值
store('upload') => 剛剛增加的配置
$fileName => 返回的是文件名
-----------------------------------------------------------------------------------
# 第二種方式
$file = $request->file('image');
# 驗證是否上傳成功
if ($file->isValid()) {
# 原文件名
$originalName = $file->getClientOriginalName();
# 擴展名
$ext = $file->getClientOriginalExtension();
# Mimetype
$type = $file->getClientMimeType();
# 臨時絕對路徑
$realPath = $file->getRealPath();
# 自定義文件名
$fileName = date('Ymd').'/'.uniqid().'.'.$ext;
# 選擇磁盤
$bool = Storage::disk('upload')->put($fileName, file_get_contents($realPath));
dd($bool);
}