thinkphp5.0 实现单文件上传功能
思路是:在app/ceshi/fire下面有一个index操作方法来渲染显示前端文件,然后前端文件跳转到upload操作方法进行处理,成功显示"文件上传成功",失败显示错误.
首先是后台 app/ceshi/fire
<?php
namespace app\ceshi\controller;
use think\Controller;
use think\Request; class Fire extends Controller{ // 文件上传表单
public function index(){
return $this->fetch();
} // 文件上传提交
public function upload(){
// 获取表单上传文件
$file = request()->file('files'); if (empty($file)) {
$this->error('请选择上传文件');
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if ($info) {
$this->success('文件上传成功');
echo $info->getFilename();
} else {
// 上传失败获取错误信息
$this->error($file->getError());
} } }
现在是前端文件:fire/index.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传示例</title>
<style>
body {
font-family:"Microsoft Yahei","Helvetica Neue",Helvetica,Arial,sans-serif;
font-size:16px;
padding:5px;
}.form{
padding: 15px;
font-size: 16px;
}.form .text {
padding: 3px;
margin:2px 10px;
width: 240px;
height: 24px;
line-height: 28px;
border: 1px solid #D4D4D4;
}.form .btn{
margin:6px;
padding: 6px;
width: 120px; font-size: 16px;
border: 1px solid #D4D4D4;
cursor: pointer;
background:#eee;
}.form .file{
margin:6px;
padding: 6px;
width: 220px; font-size: 16px;
border: 1px solid #D4D4D4;
cursor: pointer;
background:#eee;
}a{
color: #;
cursor: pointer;
}a:hover{
text-decoration: underline;
}
h2{
color: #4288ce;
font-weight: ;
padding: 6px ;
margin: 6px ;
font-size: 28px;
border-bottom: 1px solid #eee;
}div{
margin:8px;
}.info{
padding: 12px ;
border-bottom: 1px solid #eee;
}.copyright{
margin-top: 24px;
padding: 12px ;
border-top: 1px solid #eee;
}</style>
</head>
<body>
<h2>文件上传示例</h2>
<FORM method="post" enctype="multipart/form-data" class="form" action="{:url('upload')}">选择文件:
<INPUT type="file" class="files" name="files"><br/>
<INPUT type="submit" class="btn" value=" 提交 ">
</FORM>
</body>
</html>
显示效果:

文件上传成功:

thinkphp5.0 实现单文件上传功能的更多相关文章
- PHPCMS_V9 模型字段添加单文件上传功能
后台有“多文件上传”功能,但是对于有些情况,我们只需要上传一个文件,而使用多文件上传功能上传一个文件,而调用时调用一个文件URL太麻烦了. 使用说明: 1.打开phpcms\modules\conte ...
- springmvc中使用文件上传功能
项目代码:https://github.com/PeiranZhang/springmvc-fileupload Servlet3.0之前使用文件上传功能 Servlet3.0之前需要使用common ...
- spring mvc 3.0 实现文件上传功能
http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...
- Servlet3.0的文件上传功能
在Servlet3.0之前,文件上传需要借助于第三方插件,在Servlet3.0之后,Servlet本身开始支持文件上传功能. 获取上传的文件可以通过HTTPServletRequest的getPar ...
- django项目中form表单和ajax的文件上传功能。
form表单文件上传 路由 # from表单上传 path('formupload/',apply.formupload,name='formupload/'), 方法 # form表单文件上传 de ...
- yii2.0单文件上传和多文件上传
yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传: yii2单个文件上传: 上传步奏,先创建上传表单模型model(包含验证规 ...
- 配置php.ini实现PHP文件上传功能
本文介绍了如何配置php.ini实现PHP文件上传功能.其中涉及到php.ini配置文件中的upload_tmp_dir.upload_max_filesize.post_max_size等选项,这些 ...
- MVC5:使用Ajax和HTML5实现文件上传功能
引言 在实际编程中,经常遇到实现文件上传并显示上传进度的功能,基于此目的,本文就为大家介绍不使用flash 或任何上传文件的插件来实现带有进度显示的文件上传功能. 基本功能:实现带有进度条的文件上传功 ...
- Spring 文件上传功能
本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: <dependency> <groupId> ...
随机推荐
- 嵌入式开发之hi3516---GV7601 SPI通信问题
http://blog.csdn.net/qq_29350001/article/details/52669964 http://blog.csdn.net/zqj6893/article/detai ...
- Python——os(一)进程参数
python的os模块提供了一种使用操作系统相关函数的通用手段,如果只是想读或写文件请移步 open(),向操作路径请查阅 os.path 模块,如果想要读取命令行中所有文件里的所有行请查阅 file ...
- 微软office web apps 服务器搭建之在线文档预览
案例:http://owa.linbsoft.com/op/generate.aspx# 文档地址:http://demo.linbsoft.com/CourseFile/201407/2014070 ...
- C# Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005
环境说明: Win2008 R2(中文版) x64 .IIS 7.0 功能说明:上传Excel到服务器,并在服务器端读取Excel中的数据: 异常信息:Retrieving the COM class ...
- How to suppress 'Maybe this is program method' warnings from ProGuard
11down votefavorite 2 I'm using ProGuard with my Android application and I'm running getting the war ...
- spring oxm入门(包含demo)
O/X Mapper 是什么? Spring 3.0 的一个新特性是 O/X Mapper.O/X 映射器这个概念并不新鲜,O 代表 Object,X 代表 XML.它的目的是在 Java 对象(几乎 ...
- c# 连接mysql配置config,不用装net connector
<system.data> <DbProviderFactories> <remove invariant="MySql.Data.MySqlClient&qu ...
- SOFA企业应用框架
前言 从业这么多年,接触过银行的应用,Apple的应用,eBay的应用和现在阿里的应用,虽然分属于不同的公司,使用了不同的架构,但有一个共同点就是都很复杂.导致复杂性的原因有很多,如果从架构的层面看, ...
- iOS:根据系统类型加载不同的xib
1.将 xib 文件名手动更改为 xxx~iphone.xib 和 xxx~ipad.xib 2.初始化时使用 [[xxx alloc] init] 即可,系统会自动判断系统类型并加载对应的 xib ...
- css 边框使用
https://www.cnblogs.com/luka/p/5677241.html 1. 应用边框样式 先从控制边框样式的属性开始.简单边框有三个关键属性:border-width.border- ...