官方手册中关于
文件上传:thinkphp5文件上传
图像处理:thinkphp5图像处理
可放在common.php公共函数文件中的–文件上传
/*
多文件上传-不含缩略图
@prama $name 表单中name属性值
@prama $path 保存路径/public/upload/$path
*/
function uploadFile($name,$path){
// 获取表单上传文件
$files = request()->file('license');
foreach($files as $file){
$filePath = ROOT_PATH . 'public' . DS . 'upload' . DS .$path ;
if(!file_exists($filePath)){
mkdir($filePath,0777,true);
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->validate(['size'=>6291456,'ext'=>'jpeg,jpg,png,gif']) -> move($filePath);
if($info){
// 成功上传后 获取上传信息
// 输出 42a79759f284b767dfcb2a0197904287.jpg
return array('code'=>1,'msg'=>$info->getFilename());
}else{
// 上传失败获取错误信息
return array('code'=>2,'msg'=>$file->getError());
}
}
}
这是引用别人的
/*
* $name为表单上传的name值
* $filePath为为保存在入口文件夹public下面uploads/下面的文件夹名称,没有的话会自动创建
* $width指定缩略宽度
* $height指定缩略高度
* 自动生成的缩略图保存在$filePath文件夹下面的thumb文件夹里,自动创建
* @return array 一个是图片路径,一个是缩略图路径,如下:
* array(2) {
["img"] => string(57) "uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg"
["thumb_img"] => string(63) "uploads/img/thumb/20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg"
}
*/
function uploadFile($name,$filePath,$width,$height)
{
$file = request()->file($name);
if($file){
$filePaths = ROOT_PATH . 'public' . DS . 'uploads' . DS .$filePath;
if(!file_exists($filePaths)){
mkdir($filePaths,0777,true);
}
$info = $file->move($filePaths);
if($info){
$imgpath = 'uploads/'.$filePath.'/'.$info->getSaveName();
$image = \think\Image::open($imgpath);
$date_path = 'uploads/'.$filePath.'/thumb/'.date('Ymd');
if(!file_exists($date_path)){
mkdir($date_path,0777,true);
}
$thumb_path = $date_path.'/'.$info->getFilename();
$image->thumb($width, $height)->save($thumb_path);
$data['img'] = $imgpath;
$data['thumb_img'] = $thumb_path;
return $data;
}else{
// 上传失败获取错误信息
return $file->getError();
}
}
}