Laravel-文件上传
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);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理