ThinkPHP 3.2 文件上传实现概述
本示例详细解析了如何在 ThinkPHP 3.2 中实现简单的文件上传功能,供读者参考学习。
核心逻辑位于 IndexController.class.php 文件中。
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
function index(){
$this->display();
}
public function upload(){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Uploads/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
print_r($info);exit;
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功
$this->success('上传成功!');
}
}
}
?>
前端交互界面由 index.html 文件定义。
需在 View 目录下创建与控制器名称一致的 Index 文件夹,并在其中放置与方法名相同的 index.html 文件,具体目录结构此处略过。
此外,推荐一款本站提供的 PHP 代码格式化排版工具,辅助后续开发中的代码规范整理:
php代码在线格式化美化工具:点击使用
希望上述内容能为使用 ThinkPHP 框架进行 PHP 程序设计的人员提供有益帮助。