Laravel 5 教程 - 文件上传
一、简介
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 App\Http\Requests;
use Illuminate\Support\Facades\Storage; 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本地存储空间(目录)
//这里的uploads是配置文件的名称
$bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
var_dump($bool); } } return view('upload');
}
}
2-1.upload.blade.php 模板代码(上传组件为bootstrap-fileinput)如果太乱,可以看下面的最简单的页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="/static/layui-v2.1.7/css/layui.css" />
<script src="/static/layui-v2.1.7/layui.js"></script>
<link rel="stylesheet" href="/static/css/bootstrap-4.0.0-beta.2/css/bootstrap.min.css" />
<script src="/static/js/jquery/jquery-3.2.1.min.js"></script>
<script src="/static/js/popper/popper.min.js"></script>
<script src="/static/css/bootstrap-4.0.0-beta.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="/static/css/index.css" />
<link href="/static/bootstrap-fileinput/css/fileinput.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/static/css/font-awesome-4.7.0/css/font-awesome.min.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/static/bootstrap-fileinput/themes/explorer-fa/theme.css" media="all" rel="stylesheet" type="text/css"/>
<script src="/static/bootstrap-fileinput/js/plugins/sortable.js" type="text/javascript"></script>
<script src="/static/bootstrap-fileinput/js/fileinput.js" type="text/javascript"></script>
<script src="/static/bootstrap-fileinput/js/locales/zh.js" type="text/javascript"></script>
<script src="/static/bootstrap-fileinput/themes/explorer-fa/theme.js" type="text/javascript"></script>
<script src="/static/bootstrap-fileinput/themes/fa/theme.js" type="text/javascript"></script> <title>报表上传</title>
</head>
<body> <div class="layui-body"> <div style="padding: 15px;">
<blockquote class="layui-elem-quote">
报表上传
</blockquote>
</div> <div class="container"> <div class="container kv-main">
<form enctype="multipart/form-data" method="post">
<label class="control-label">Select File</label>
<input id="input-b5" name="input-b5" type="file" multiple>
{{ csrf_field() }}
</form> </div> </div> </div> </body>
<script> $(document).ready(function(){
$("#input-b5").fileinput({
showCaption: false,
theme: 'fa',
language: 'zh',
uploadUrl: './upload',
allowedFileExtensions: ['jpg', 'png', 'gif']
});
});
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
}); </script> </html>
2-2. 最基础的 upload.blade.php 模板代码:
<form method="post" enctype="multipart/form-data" >
<input type="file" name="picture">
<button type="submit"> 提交 </button>
</form>
Laravel 5 教程 - 文件上传的更多相关文章
- Laravel 5.2 教程 - 文件上传
一.简介 Laravel 有很棒的文件系统抽象层,是基于 Frank de Jonge 的 Flysystem 扩展包. Laravel 集成的 Flysystem 提供了简单的接口,可以操作本地端空 ...
- php laravel 帧 该文件上传
好,我承认我的忠告. 今天laravel框架编写一个文件上传部分.总能找到不正确的路径.但是,终于攻克. 以下我分享一下自己的学习体会吧. client <form method="P ...
- PHP高级教程-文件上传
PHP 文件上传 通过 PHP,可以把文件上传到服务器. 本章节实例在 test 项目下完成,目录结构为: test |-----upload # 文件上传的目录 |-----form.html # ...
- Laravel请求/Cookies/文件上传
一.HTTP请求 1.基本示例:通过依赖注入获取当前 HTTP 请求实例,应该在控制器的构造函数或方法中对Illuminate\Http\Request 类进行类型提示,当前请求实例会被服务容器自动注 ...
- Spring Boot 教程 - 文件上传下载
在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...
- laravel中的文件上传到本地+七牛云上传
首先在filesystems.php 配置好上传的文件的目录起名为upload 在Storage/目录下面 目录下面的app/upload 如果没有这个文件会自动创建 这里的名字upload名字是跟控 ...
- IT兄弟连 JavaWeb教程 文件上传技术
在Web应用系统开发中,文件上传和下载功能是非常常用的功能. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参 ...
- Laravel - 上手实现 - 文件上传、保存到 public 目录下
1.为了访问方便,将上传的文件保存在 public 目录下,需要进行修改配置. 找到 config/filesystems.php 文件然后修改 root.具体如下: 'local' => [ ...
- laravel oauth2.0 文件上传报错
报错信息: "message": "Invalid stream or file provided for UploadedFile", " ...
随机推荐
- ORACLE启动 切换实例命令
启动服务器的其他实例 export ORACLE_SID=数据库实例名 sqlplus /nolog conn /as sysdba select name from v$database; !lsn ...
- <摘录>Linux 环境下编译 0.11版本内核 kernel
系统环境:Fedora 13 + gcc-4.4.5 最近在看<linux内核0.11完全注释>一书,由于书中涉及汇编语言的地方众多,本人在大学时汇编语言学得一塌糊涂,所以实在看不下去了, ...
- rust安装
http://blog.csdn.net/teamlet/article/details/50838996
- CSS揭秘之多重边框&连续的图像边框
1.多重边框 我们可以通过使用border-image来写一个多重边框,或使用多个元素来模拟多重边框,不过我们有更好的办法来制作一个多重边框,那就是使用box-shadow的第四个参数(称为扩张半径) ...
- Unity3d插件研究之Easytouch
但我们开发移动端的游戏时,发现使用Input.GetMouseButtonDown的方法不可用,怎么办? 虽然unity3d也有自带触屏的方法,但是使用起来代价太高,什么单击,双击这些功能都要自己封装 ...
- hive删除列
hive中删除列时没有与mysql语句alter table <table> drop column <col>对应的语句. 然而依然可以完成此功能:使用ALTER TABLE ...
- Bootstrap导航栏实例讲解
导航栏是一个很好的功能,是 Bootstrap 网站的一个突出特点.导航栏是响应式元组件就,作为应用程序或网站的导航标题.导航栏在移动设备的视图中是折叠的,随着可用视口宽度的增加,导航栏也会水平展开. ...
- latex 三个不同的图放在一行且每个图都有注释
\begin{figure}[htbp] \begin{minipage}[t]{0.3\linewidth} \centering \includegraphics[width=.2.0.eps} ...
- OCP试题解析之053-17 CONFIGURE CONTROLFILE AUTOBACKUP ON
17.You configure AUTOBACKUP to ON in an RMAN session. When will RMAN back up the control file? (Choo ...
- RenderMonkey 练习 第二天 【opengl 光照模型】
光照模型 3D渲染中, 物体表面的光照计算公式为: I = 环境光(Iambient) + 漫反射光(Idiffuse) + 镜面高光(Ispecular); 其中,环境光(ambient)计算公式为 ...