栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP Laravel 上传图片、文件等类封装

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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(),//是裁剪压缩比例 
  ); 
  public function __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']; 
  } 
  public function __get($name) { 
    return $this->config[$name]; 
  } 
  public function __set($name,$value){ 
    if(isset($this->config[$name])) { 
      $this->config[$name] = $value; 
    } 
  } 
  public function __isset($name){ 
    return isset($this->config[$name]); 
  } 
   
  public function getError(){ 
    return $this->error; 
  } 
  public function upload($file){ 
     if(emptyempty($file)){ 
$this->error = '没有上传的文件'; 
return false; 
     } 
     if(!$this->checkRootPath($this->fullPath)){ 
$this->error = $this->getError(); 
return false; 
     } 
     $fileSavePath=$this->fullPath.$this->savePath.$this->subName; 
     if(!$this->checkSavePath($fileSavePath)){ 
$this->error = $this->getError(); 
return false; 
     } 
     $files =array(); 
     if(!is_array($file)){ 
//如果不是数组转成数组 
$files[]=$file; 
     }else{ 
$files=$file; 
     } 
    $info  = array(); 
     $imgThumb = new AppThinkClassThumbClass(); 
     foreach ($files as $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; 
} 
     } 
     return is_array($info) ? $info : false; 
  } 
   
  protected function checkRootPath($rootpath){ 
    if(!(is_dir($rootpath) && is_writable($rootpath))){ 
      $this->error = '上传根目录不存在!'; 
      return false; 
    } 
    return true; 
  } 
   
  public function checkSavePath($savepath){ 
     
    if (!$this->mkdir($savepath )) { 
      return false; 
    } else { 
       
      if (!is_writable($savepath)) { 
 $this->error = '上传目录不可写!'; 
 return false; 
      } else { 
 return true; 
      } 
    } 
  } 
   
  private function check($file) { 
     
    if (!$this->checkSize($file->getSize())) { 
      $this->error = '上传文件大小不符!'; 
      return false; 
    } 
     
    if (!$this->checkExt($file->ext)) { 
      $this->error = '上传文件后缀不允许'; 
      return false; 
    } 
     
    return true; 
  } 
   
  private function checkSize($size) { 
    return !($size > $this->maxSize) || (0 == $this->maxSize); 
  } 
   
  private function checkExt($ext) { 
    return emptyempty($this->config['exts']) ? true : in_array(strtolower($ext), $this->exts); 
  } 
   
  protected function mkdir($savepath){ 
    if(is_dir($savepath)){ 
      return true; 
    } 
    if(mkdir($savepath, 0777, true)){ 
      return true; 
    } else { 
      $this->error = "目录创建失败"; 
      return false; 
    } 
  } 
}

使用案例:

头部引用  use AppThinkClassUploadClass; 

$upload = new UploadClass(); 
$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);

总结

以上所述是小编给大家介绍的PHP Laravel 上传图片、文件等类封装,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/40497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号