配置
文件系统的配置文件在 config/filesystems.php 文件中,此处我们新建一个uploads本地磁盘空间用于存储上传的文件,具体配置项及说明如下:
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. A "local" driver, as well as a variety of cloud
| based drivers are available for your choosing. Just store away!
|
| Supported: "local", "ftp", "s3", "rackspace"
|
*/
// 默认使用本地端空间 支持 "local", "ftp", "s3", "rackspace"
'default' => '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.
|
*/
// 云存储使用 Amazon S3
'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.
|
*/
'disks' => [
// 本地端的local空间
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
// 本地端的public空间
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'visibility' => 'public',
],
// 新建一个本地端uploads空间(目录) 用于存储上传的文件
'uploads' => [
'driver' => 'local',
// 文件将上传到storage/app/uploads目录
'root' => storage_path('app/uploads'),
// 文件将上传到public/uploads目录 如果需要浏览器直接访问 请设置成这个
//'root' => public_path('uploads'),
],
// Amazon S3 相关配置
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
],
];
三、代码实现文件上传
1. 控制器代码
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Storage;
use AppHttpRequests;
class FileController extends Controller
{
// 文件上传方法
public function upload(Request $request)
{
if ($request->isMethod('post')) {
$file = $request->file('picture');
// 文件是否上传成功
if ($file->isValid()) {
// 获取文件相关信息
$originalName = $file->getClientOriginalName(); // 文件原名
$ext = $file->getClientOriginalExtension(); // 扩展名
$realPath = $file->getRealPath(); //临时文件的绝对路径
$type = $file->getClientMimeType(); // image/jpeg
// 上传文件
$filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
// 使用我们新建的uploads本地存储空间(目录)
$bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
var_dump($bool);
}
}
return view('upload');
}
}
2. 最基础的 upload.blade.php 模板代码:
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级