这是一份源码php
<?php namespace app\data\util; use think\helper\Str; class UploadUtil { /** * @var 文件信息think\file这个类 */ private $file; /** * @var 上传图片的目录 */ private $path; /** * 上传文件规则 */ // protected $validate =[ // 'size' => 500000, // 'ext' => 'jpg,png,gif,jpeg', // ]; /** * 文件上传 * * @param file think\File * @path 上传的目录 upload\goods * @return array */ public function move($file,$path) { $this->file = $file; // 获取上传的文件名 $fileName = $this->getFileName($path); // 文件保存的地址 $save = $this->getFilePath($path); $save = str_replace('application/','',$save); // 判断保存的目录是否存在 if(!file_exists($save)){ mkdir($save,0777,true); } // 文件保存后的名字加类型 $image = $fileName['saveName'].'.'.$fileName['fileSuffix']; // 开始上传 参数一:上传路径 参数二:文件名 $info = $file->validate(['size'=>20000000,'ext'=>'jpg,png,gif'])->move($save,$image); // 获取上传后的文件名 $this->path = $path.'/'.$image; # 去除public $this->path = str_replace('/public','',$this->path); if($info){ $resule = ['code'=>SUCCESS,'data'=>$this->path]; }else{ $resule = ['code'=>ERROR,'data'=>$file->getError()]; } return $resule; } /** * 配置保存路径 * * @return array */ public function getFilePath($path) { return ROOT.'/'.$path; } /** * 获取上传文件的信息 名字,类型,类型 * * @return array */ public function getFileName() { // 获取文件信息 $name = $this->file->getInfo('name'); // 问件名1.jpg 因此须要转数组获取 $fileName = explode('.',$name); return [ // 文件名 'formerlyName' => $fileName[0], // 保存后的文件名 'saveName' => $fileName[0].time(), // 文件后缀 'fileSuffix' => $fileName[1] ]; } /** * 保存后的文件路径 * * @return array */ public function functionName($flag = true) { return ($flag) ? $this->path : ROOT.'/'.$this->path; } }
在common文件中定义一个常量
而后在配置文件里边定义
数组
调用
app