Laravel 5.2 教程 - 文件上传
一、简介
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 App\Http\Controllers;
use Illuminate\Http\Request;
use Storage;
use App\Http\Requests;
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>
获取到文件后,即可对文件进行各种处理。如果是图片,可以进行各种缩放及裁剪操作。
交友互动:
本文首发于马燕龙个人博客,欢迎分享,转载请标明出处。
马燕龙个人博客:http://www.mayanlong.com
马燕龙个人微博:http://weibo.com/imayanlong
马燕龙Github主页:https://github.com/yanlongma
Laravel 5.2 教程 - 文件上传的更多相关文章
- Laravel 5 教程 - 文件上传
一.简介 Laravel 有很棒的文件系统抽象层,是基于 Frank de Jonge 的 Flysystem 扩展包. Laravel 集成的 Flysystem 提供了简单的接口,可以操作本地端空 ...
- PHP高级教程-文件上传
PHP 文件上传 通过 PHP,可以把文件上传到服务器. 本章节实例在 test 项目下完成,目录结构为: test |-----upload # 文件上传的目录 |-----form.html # ...
- Spring Boot 教程 - 文件上传下载
在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...
- IT兄弟连 JavaWeb教程 文件上传技术
在Web应用系统开发中,文件上传和下载功能是非常常用的功能. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参 ...
- laravel 7七牛云文件上传
1:composer下载七牛云 composer require itbdw/laravel-storage-qiniu 2:打开 config 文件夹下的 app.php 文件,在 provider ...
- Laravel 学习笔记之文件上传
自定义添加磁盘——upload 位置:config/filesystems.php 'disks' => [ 'local' => [ 'driver' => 'local', 'r ...
- laravel之文件上传
laravel框架中的文件上传我们应该如何实现此功能呢? 之前也是没有使用过laravel的文件上传功能,后来在网上找到一些教程,五花八门.让我看起来有点头疼. 有时候找到测试浪费好长时间最后还是出不 ...
- php laravel 帧 该文件上传
好,我承认我的忠告. 今天laravel框架编写一个文件上传部分.总能找到不正确的路径.但是,终于攻克. 以下我分享一下自己的学习体会吧. client <form method="P ...
- SpringBoot文件上传异常之提示The temporary upload location xxx is not valid
原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...
随机推荐
- python 解析Excel
python 解析Excel 公司背景:好吧LZ太懒了.略... 原由起因:公司老板发话要导出公司数据库中符合条件的数据,源数据有400万,符合条件的大概有70万左右吧. 最终目的:符合条件的数据并生 ...
- django进阶-4
前言: 下篇博客写关于bootstrap... 一.如何在脚本测试django from django.db import models class Blog(models.Model): name ...
- windos环境apache+mysql+php+Discuz的安装配置
首先是相关软件的下载:PHP.Apache和Mysql软件以及VC库.相关软件可到我的百度网盘下载,百度网盘:http://pan.baidu.com/s/1o6DYcMu 相关软件的直接下载地址: ...
- python混账的编码问题解决之道
下面的代码作用是修改文件的编码格式.代码很简单,但是也很牛逼(在我看来),这是在segment上找到的解决办法,废话不多说,直接上代码. import codecsdef ReadFile(fileP ...
- MySQL---事务知识,你搞明白没有?
MySQL - 事务 在学习事务这一概念前,我们需要需要构思一个场景 场景构思 假设该场景发生于一个银行转账背景下,月中,又到了发工资的日子.潭州教育科技集团打算给Tuple老师发放一个月的工资.(此 ...
- 关于压缩jar包时提示*.*没有这个文件或目录的问题以及解决办法:
关于压缩jar包时提示.没有这个文件或目录的问题以及解决办法: 问题描述: 我在打包jar时,CMD中进入到包的上一层目录. 在命令提示符中输入 提示如下: 从提示中可知没有找到我们想要打包的clas ...
- 读书笔记 effective c++ Item 42 理解typename的两种意义
1. class和typename意义相同的例子 问题:在下面的模板声明中class和typename的区别是什么? template<class T> class Widget; // ...
- GCM(Google Cloud Messaging)推送完全解析
应该是目前为止全网最详细的GCM推送解析. GCM官方解释: https://developers.google.cn/cloud-messaging/?hl=zh-cn Send data from ...
- 性能测试培训:tomcat性能调优方法
性能测试培训:tomcat性能调优方法 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loadrunner ...
- 性能调优:mysql之left join
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...