Laravel 教程 - 文件上传
一、简介
Laravel 有很棒的文件系统抽象层,是基于 Frank de Jonge 的 Flysystem 扩展包。 Laravel 集成的 Flysystem 提供了简单的接口,可以操作本地端空间、 Amazon S3 、 Rackspace Cloud Storage 。更方便的是,它可以非常简单的切换不同保存方式,但仍使用相同的 API 操作!
默认使用本地端空间。当然,你也可以设置多组磁盘,甚至在多个磁盘使用相同的驱动。Laravel文件系统提供了非常强大的功能,但是本文只介绍常用的文件上传功能。
本文通过介绍使用本地端空间来介绍Laravel中文件上传的使用。
二、配置
文件系统的配置文件在 config/filesystems.php 文件中,此处我们新建一个uploads本地磁盘空间用于存储上传的文件,具体配置项及说明如下:
<?php
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. 控制器代码
<?php
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 模板代码:
<form method="post" enctype="multipart/form-data" >
<input type="file" name="picture">
<button type="submit"> 提交 </button>
</form>
获取到文件后,即可对文件进行各种处理。如果是图片,可以进行各种缩放及裁剪操作。
系统需求
- PHP >= 5.3
- Fileinfo Extension
- GD Library (>=2.0) … or …
- Imagick PHP extension (>=6.5.7)
安装部署 Integration/image
在 composer.json [require] 节增加,之后执行 composer update
"intervention/image": "2.0.15"
Laravel 配置
安装部署 Integration/image 完成后,打开配置文件 config/app.php 在相应位置添加代码,然后 Image 类就能自动加载并可供使用了。其功能强大到可以处理你的几乎所有图片处理需求。
//服务提供器 "InterventionImageImageServiceProvider" //别名配置 "Image" => "InterventionImageFacadesImage"
配置设置
默认情况下, Integration/Image 使用PHP的GD库扩展。如果你想切换到 imagick,你可以使用 php artisan 创建一个配置文件以添加相应的配置。
$ php artisan config:publish intervention/imag
基本使用
这里列出几个基本功能,更详细使用说明请查看相关接口文档。1、显示一张图片
Route::get("/", function() { $img = Image::make("foo.jpg")->resize(300, 200); return $img->response("jpg"); });
2、读取一个图片文件
$img = Image::make("foo/bar/baz.jpg");
3、绘制一张图片
$img = Image::canvas(800, 600, "#ccc");
4、编辑一张图片
$img = Image::make("foo.jpg")->resize(320, 240)->insert("watermark.png");
Laravel文件上传图片
public function onPublishStudentHomework(){// $params = explode("/", Request::getRequestUri());
// $studentHomeworkId = last($params);
$user = new User();
$userInfo = $user->getUser();
$description = post("description");
$studentHomeworkId = $this->param("id");
$uploads = post("uploadImage");
if(!empty($uploads)){
foreach ($uploads as $key => $upload) {
//trace_log($upload);exit;
$substr = explode("homework/",$upload);
$image = Storage::disk("uploads")->get($substr[1]);
$fileSize = Storage::disk("uploads")->size($substr[1]);
$imageType = Storage::disk("uploads")->mimeType($substr[1]);
$updateTime = Storage::disk("uploads")->lastModified($substr[1]);
Db::table("dapeng_student_homework_resources")->insert([
"student_homework_id" => $studentHomeworkId,
"disk_name" => $upload,
"file_name" => $substr[1],
"file_size" => $fileSize,
"content_type" =>$imageType,
"created_at" => date("Y-m-d-H-i-s",$updateTime),
]);
Db::table("dapeng_student_homework")->where("id",$studentHomeworkId)->update(["description" => $description,"is_submit" => 1, "submit_at" => date("Y-m-d-H-i-s",$updateTime)]);
}
}else{
return CommonConst::getErrorChineseDesc(CommonConst::UPLOAD_FILE_FAILE);
}
return CommonConst::getErrorChineseDesc(CommonConst::SUCCESS);
}
通过Storage来进行对数据进行操作
在我理解
第一:进行filesystem下面进行disk-> s3、local、rackspace进行存储操作、然后通过url路径来找到、然后在上传到阿里云服务器上,进行读取。
- 上一篇: PHP 统计实时统计汉字个数和区别
- 下一篇: PHP重点难点算法与面试