PHP Laravel 上传图片、文件等类封装
今天把项目中上传功能封装成类,方便后面使用,简单的封装了一下,感觉还不怎么好,后面继续优化。
具体代码如下:
3*1024*1024,//上传的文件大小限制(0-不做限制) 'exts'=>array('jpg','jpeg','gif','png','doc','docx','xls','xlsx','ppt','pptx','pdf','rar','zip'),//允许上传的文件后缀 'subName'=>'',//子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组 'rootPath'=>'/uploads/',//保存根路径 'savePath'=>'',//保存路径 'thumb'=>array(),//是裁剪压缩比例 ); publicfunction__construct($config=array()){ /*获取配置*/ $this->config=array_merge($this->config,$config); if(!emptyempty($this->config['exts'])){ if(is_string($this->exts)){ $this->config['exts']=explode(',',$this->exts); } $this->config['exts']=array_map('strtolower',$this->exts); } $this->config['subName']=$this->subName?ltrim($this->subName,'/'):'/'.date('Ymd'); $this->fullPath=rtrim(public_path(),'/').$this->config['rootPath']; } publicfunction__get($name){ return$this->config[$name]; } publicfunction__set($name,$value){ if(isset($this->config[$name])){ $this->config[$name]=$value; } } publicfunction__isset($name){ returnisset($this->config[$name]); } /** *获取最后一次上传错误信息 *@returnstring错误信息 */ publicfunctiongetError(){ return$this->error; } publicfunctionupload($file){ if(emptyempty($file)){ $this->error='没有上传的文件'; returnfalse; } if(!$this->checkRootPath($this->fullPath)){ $this->error=$this->getError(); returnfalse; } $fileSavePath=$this->fullPath.$this->savePath.$this->subName; if(!$this->checkSavePath($fileSavePath)){ $this->error=$this->getError(); returnfalse; } $files=array(); if(!is_array($file)){ //如果不是数组转成数组 $files[]=$file; }else{ $files=$file; } $info=array(); $imgThumb=new\App\ThinkClass\ThumbClass(); foreach($filesas$key=>$f){ $this->file=$f; $f->ext=strtolower($f->getClientOriginalExtension()); /*文件上传检查*/ if(!$this->check($f)){ continue; } $fileName=str_random(12).'.'.$f->ext; /*保存文件并记录保存成功的文件*/ if($this->file->move($fileSavePath,$fileName)){ /*图片按照宽高比例压缩*/ \Log::notice($fileSavePath.$fileName); if(!emptyempty($this->thumb)&&is_array($this->thumb)){ $imgThumb->thumb($this->thumb,$fileSavePath.'/'.$fileName); } $info[]=$this->rootPath.$this->savePath.$this->subName.'/'.$fileName; } } returnis_array($info)?$info:false; } /** *检测上传根目录 *@paramstring$rootpath根目录 *@returnbooleantrue-检测通过,false-检测失败 */ protectedfunctioncheckRootPath($rootpath){ if(!(is_dir($rootpath)&&is_writable($rootpath))){ $this->error='上传根目录不存在!'; returnfalse; } returntrue; } /** *检测上传目录 *@paramstring$savepath上传目录 *@returnboolean检测结果,true-通过,false-失败 */ publicfunctioncheckSavePath($savepath){ /*检测并创建目录*/ if(!$this->mkdir($savepath)){ returnfalse; }else{ /*检测目录是否可写*/ if(!is_writable($savepath)){ $this->error='上传目录不可写!'; returnfalse; }else{ returntrue; } } } /** *检查上传的文件 *@paramarray$file文件信息 */ privatefunctioncheck($file){ /*检查文件大小*/ if(!$this->checkSize($file->getSize())){ $this->error='上传文件大小不符!'; returnfalse; } /*检查文件后缀*/ if(!$this->checkExt($file->ext)){ $this->error='上传文件后缀不允许'; returnfalse; } /*通过检测*/ returntrue; } /** *检查文件大小是否合法 *@paraminteger$size数据 */ privatefunctioncheckSize($size){ return!($size>$this->maxSize)||(0==$this->maxSize); } /** *检查上传的文件后缀是否合法 *@paramstring$ext后缀 */ privatefunctioncheckExt($ext){ returnemptyempty($this->config['exts'])?true:in_array(strtolower($ext),$this->exts); } /** *创建目录 *@paramstring$savepath要创建的穆里 *@returnboolean创建状态,true-成功,false-失败 */ protectedfunctionmkdir($savepath){ if(is_dir($savepath)){ returntrue; } if(mkdir($savepath,0777,true)){ returntrue; }else{ $this->error="目录创建失败"; returnfalse; } } }
使用案例:
头部引用 useApp\ThinkClass\UploadClass;
$upload=newUploadClass(); $upload->exts=array('jpg','png'); $upload->maxSize=5*1024*1024; $upload->savePath='course/uid_6'; $file=$request->file('fileImg'); $aa=$upload->upload($file); dd($aa);
总结
以上所述是小编给大家介绍的PHPLaravel上传图片、文件等类封装,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。