ThinkPHP6.0上传文件,官方说了:内置的上传只是上传到本地服务器,上传到远程或者第三方平台的话需要安装额外的扩展。
本案例是上传到本地服务器,需要远程上传到第三方平台需要安装额外的扩展,以下是控制器中直接对文件处理,使用了Db类库,在数据表中存储了记录,功能待自行扩展,勿怪。
而且测试上传使用的是Kindeditor编辑器上传,可上传图片,视频,flash,文件,简单处理了图片按钮只能上传图片,视频类似,flash类似,文件类似。请参考:https://www.kancloud.cn/manual/thinkphp6_0/1037639
<?php namespace app\controller; use app\BaseController; use think\facade\Db; class Attachment extends BaseController { public function upload(){ if(request()->isPost()){ // 获取表单上传文件 例如上传了001.jpg $file = request()->file('imgFile');//根据表单name替换imgFile try { $dir_name = request()->param('dir');//kindeditor上传 获取上传类型 //分类定义允许上传的文件 $ext_arr = array( 'image' => array('gif','jpg','jpeg','png','bmp'), 'flash' => array('flv','swf'), 'media' => array('swf','flv','mp3','wav','wma','wmv','mid','avi','mpg','asf','rm','rmvb','mp4'), 'file' => array('doc','docx','xls','xlsx','ppt','htm','html','txt','zip','rar','gz','bz2'), ); if (empty($ext_arr[$dir_name])) { return false; }else{ $allowext = implode(',', $ext_arr[$dir_name]); } // 使用验证器验证上传的文件 validate(['file' => [ // 限制文件大小(单位b),这里限制为4M 'fileSize' => 4 * 1024 * 1024, // 限制文件后缀,多个后缀以英文逗号分割 'fileExt' => $allowext, ]])->check(['file' => $file]); // 上传到本地服务器 $savename = \think\facade\Filesystem::disk('public')->putFile($dir_name, $file, 'sha1'); if($savename){ // 拼接路径 $path=\think\Facade\Filesystem::getDiskConfig('public', 'url').str_replace('\\', '/', '/'.$savename); $data['filepath'] = $path; $data['filename'] = $file->getOriginalName(); $data['fileext'] = $file->extension(); $data['authcode'] = $file->hash('sha1'); $data['status'] = 1; $data['filesize'] = $file->getSize(); $data['downloads'] = 0; $data['uploadtime'] = time(); $data['uploadip'] = request()->ip(); if(in_array($data['fileext'], $ext_arr['image'])){ $data['isimage'] = 1; }else{ $data['isimage'] = 0; } $res=Db::name('attachment')->order('aid', 'desc')->insert($data); if($res){ return json(['error'=>0, 'url'=>$path, 'message'=>'添加成功']); }else{ return json(['error'=>1, 'url'=>'', 'message'=>'添加失败']); } } // echo $savename; } catch (think\exception\ValidateException $e) { return json(['error'=>1, 'url'=>'', 'message'=>$e->getMessage()]); }catch (\Exception $e) { return json(['error'=>1, 'url'=>'', 'message'=>$e->getMessage()]); } } } }
静态模板代码,引入了Kindeditor编辑器js等文件,初始化了编辑器,只要引入文件上面代码可以直接使用。
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>Default Examples</title> <style> form { margin: 0; } textarea { display: block; } </style> <link rel="stylesheet" href="__STATIC__/kindeditor/themes/default/default.css" /> <script charset="utf-8" src="__STATIC__/kindeditor/kindeditor-all.js"></script> <script charset="utf-8" src="__STATIC__/kindeditor/lang/zh-CN.js"></script> <script> var editor; KindEditor.ready(function(K) { editor = K.create('textarea[name="content"]', { allowFileUpload : true, allowFileManager : true, allowFlashUpload : true, uploadJson : '{:url('attachment/upload')}', }); }); </script> </head> <body> <h3>默认模式</h3> <form> <textarea name="content" style="width:800px;height:400px;visibility:hidden;">KindEditor</textarea> </form> </body> </html>
注意事项(模板变量配置位置:/vendor/topthink/think-template/src/Template.php中查找变量自行配置),需隐藏入口文件index.php(单模块):
'tpl_replace_string' => [ '__THEMES__'=>'/static/frontend/theme', '__STATIC__'=>'/static', '__JS__' => '/static/javascript', ],
数据表字典:
/* Navicat Premium Data Transfer Source Server : thinkphp_images Source Server Type : MySQL Source Server Version : 50645 Source Host : 114.215.138.183:3516 Source Schema : thinkphp_images Target Server Type : MySQL Target Server Version : 50645 File Encoding : 65001 Date: 05/04/2020 02:50:48 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for tp_attachment -- ---------------------------- DROP TABLE IF EXISTS `tp_attachment`; CREATE TABLE `tp_attachment` ( `aid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `filename` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `filepath` char(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `filesize` int(10) UNSIGNED NOT NULL DEFAULT 0, `fileext` char(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `isimage` tinyint(1) UNSIGNED NOT NULL DEFAULT 0, `downloads` mediumint(8) UNSIGNED NOT NULL DEFAULT 0, `uploadtime` int(10) UNSIGNED NOT NULL DEFAULT 0, `uploadip` char(15) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '', `status` tinyint(1) NOT NULL DEFAULT 0, `authcode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`aid`) USING BTREE, INDEX `authcode`(`authcode`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; SET FOREIGN_KEY_CHECKS = 1;
发表评论 取消回复