Home >Backend Development >PHP Tutorial >Detailed explanation of encapsulation of uploading images, files and other classes in Laravel in PHP

Detailed explanation of encapsulation of uploading images, files and other classes in Laravel in PHP

墨辰丷
墨辰丷Original
2018-05-18 14:16:263521browse

This article mainly introduces the implementation code of PHP Laravel uploading pictures, files and other class encapsulation. Friends in need can refer to it

The specific code is as follows:

<?php 
/** 
 * Created by PhpStorm. 
 * User: wady www.bcty365.com 
 * Date: 2017/8/16 
 * Time: 14:52 
 */ 
namespace App\ThinkClass; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 
class UploadClass 
{ 
  /** 
   * @var UploadedFile $file; 
   */ 
  protected $file; 
  /** 
   * 上传错误信息 
   * @var string 
   */ 
  private $error = &#39;&#39;; //上传错误信息 
  private $fullPath=&#39;&#39;;//绝对地址 
  private $config = array( 
    &#39;maxSize&#39;    => 3*1024*1024, //上传的文件大小限制 (0-不做限制) 
    &#39;exts&#39;     => array(&#39;jpg&#39;,&#39;jpeg&#39;,&#39;gif&#39;,&#39;png&#39;,&#39;doc&#39;,&#39;docx&#39;,&#39;xls&#39;,&#39;xlsx&#39;,&#39;ppt&#39;,&#39;pptx&#39;,&#39;pdf&#39;,&#39;rar&#39;,&#39;zip&#39;), //允许上传的文件后缀 
    &#39;subName&#39;    => &#39;&#39;, //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组 
    &#39;rootPath&#39;   => &#39;/uploads/&#39;, //保存根路径 
    &#39;savePath&#39;   => &#39;&#39;, //保存路径 
    &#39;thumb&#39;     => array(),//是裁剪压缩比例 
  ); 
  public function __construct($config = array()){ 
    /* 获取配置 */ 
    $this->config  =  array_merge($this->config, $config); 
    if(!emptyempty($this->config[&#39;exts&#39;])){ 
      if (is_string($this->exts)){ 
        $this->config[&#39;exts&#39;] = explode(&#39;,&#39;, $this->exts); 
      } 
      $this->config[&#39;exts&#39;] = array_map(&#39;strtolower&#39;, $this->exts); 
    } 
    $this->config[&#39;subName&#39;] = $this->subName ? ltrim($this->subName,&#39;/&#39;) : &#39;/&#39;.date(&#39;Ymd&#39;); 
    $this->fullPath = rtrim(public_path(),&#39;/&#39;).$this->config[&#39;rootPath&#39;]; 
  } 
  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]); 
  } 
  /** 
   * 获取最后一次上传错误信息 
   * @return string 错误信息 
   */ 
  public function getError(){ 
    return $this->error; 
  } 
  public function upload($file){ 
     if(emptyempty($file)){ 
       $this->error = &#39;没有上传的文件&#39;; 
       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 \App\ThinkClass\ThumbClass(); 
     foreach ($files as $key=>$f){ 
       $this->file=$f; 
        $f->ext = strtolower($f->getClientOriginalExtension()); 
       /*文件上传检查*/ 
       if (!$this->check($f)){ 
         continue; 
       } 
       $fileName = str_random(12).&#39;.&#39;.$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.&#39;/&#39;.$fileName); 
         } 
         $info[]=$this->rootPath.$this->savePath.$this->subName.&#39;/&#39;.$fileName; 
       } 
     } 
     return is_array($info) ? $info : false; 
  } 
  /** 
   * 检测上传根目录 
   * @param string $rootpath  根目录 
   * @return boolean true-检测通过,false-检测失败 
   */ 
  protected function checkRootPath($rootpath){ 
    if(!(is_dir($rootpath) && is_writable($rootpath))){ 
      $this->error = &#39;上传根目录不存在!&#39;; 
      return false; 
    } 
    return true; 
  } 
  /** 
   * 检测上传目录 
   * @param string $savepath 上传目录 
   * @return boolean     检测结果,true-通过,false-失败 
   */ 
  public function checkSavePath($savepath){ 
    /* 检测并创建目录 */ 
    if (!$this->mkdir($savepath )) { 
      return false; 
    } else { 
      /* 检测目录是否可写 */ 
      if (!is_writable($savepath)) { 
        $this->error = &#39;上传目录不可写!&#39;; 
        return false; 
      } else { 
        return true; 
      } 
    } 
  } 
  /** 
   * 检查上传的文件 
   * @param array $file 文件信息 
   */ 
  private function check($file) { 
    /* 检查文件大小 */ 
    if (!$this->checkSize($file->getSize())) { 
      $this->error = &#39;上传文件大小不符!&#39;; 
      return false; 
    } 
    /* 检查文件后缀 */ 
    if (!$this->checkExt($file->ext)) { 
      $this->error = &#39;上传文件后缀不允许&#39;; 
      return false; 
    } 
    /* 通过检测 */ 
    return true; 
  } 
  /** 
   * 检查文件大小是否合法 
   * @param integer $size 数据 
   */ 
  private function checkSize($size) { 
    return !($size > $this->maxSize) || (0 == $this->maxSize); 
  } 
  /** 
   * 检查上传的文件后缀是否合法 
   * @param string $ext 后缀 
   */ 
  private function checkExt($ext) { 
    return emptyempty($this->config[&#39;exts&#39;]) ? true : in_array(strtolower($ext), $this->exts); 
  } 
  /** 
   * 创建目录 
   * @param string $savepath 要创建的穆里 
   * @return boolean     创建状态,true-成功,false-失败 
   */ 
  protected function mkdir($savepath){ 
    if(is_dir($savepath)){ 
      return true; 
    } 
    if(mkdir($savepath, 0777, true)){ 
      return true; 
    } else { 
      $this->error = "目录创建失败"; 
      return false; 
    } 
  } 
}

Use case:

Header reference use App\ThinkClass\UploadClass;

$upload = new UploadClass(); 
$upload->exts=array(&#39;jpg&#39;,&#39;png&#39;); 
$upload->maxSize=5*1024*1024; 
$upload->savePath=&#39;course/uid_6&#39;; 
$file = $request->file(&#39;fileImg&#39;); 
$aa = $upload->upload($file); 
dd($aa);

Related recommendations:

Laravel multi-user authentication system use case analysis

Laravel model event implementation steps detailed explanation

laravelFramework implements search function code analysis

The above is the detailed content of Detailed explanation of encapsulation of uploading images, files and other classes in Laravel in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn