excel if函數(shù)輸出公式如何寫
923
2023-11-14
今天把項(xiàng)目中上傳功能封裝成類,方便后面使用,簡單的封裝了一下,感覺還不怎么好,后面繼續(xù)優(yōu)化。
具體代碼如下:
<?php /** * Created by PhpStorm. * User: wady www.bcty365.com * Date: 2017/8/16 * Time: 14:52 */ namespace AppThinkClass; use SymfonyComponentHttpFoundationFileUploadedFile; class UploadClass { /** * @var UploadedFile $file; */ protected $file; /** * 上傳錯(cuò)誤信息 * @var string */ private $error = ''; //上傳錯(cuò)誤信息 private $fullPath='';//絕對地址 private $config = array( 'maxSize' => 3*1024*1024, //上傳的文件大小限制 (0-不做限制) 'exts' => array('jpg','jpeg','gif','png','doc','docx','xls','xlsx','ppt','pptx','pdf','rar','zip'), //允許上傳的文件后綴 'subName' => '', //子目錄創(chuàng)建方式,[0]-函數(shù)名,[1]-參數(shù),多個(gè)參數(shù)使用數(shù)組 '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]); } /** * 獲取最后一次上傳錯(cuò)誤信息 * @return string 錯(cuò)誤信息 */ 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)){ //如果不是數(shù)組轉(zhuǎn)成數(shù)組 $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; } /** * 檢測上傳根目錄 * @param string $rootpath 根目錄 * @return boolean true-檢測通過,false-檢測失敗 */ protected function checkRootPath($rootpath){ if(!(is_dir($rootpath) && is_writable($rootpath))){ $this->error = '上傳根目錄不存在!'; return false; } return true; } /** * 檢測上傳目錄 * @param string $savepath 上傳目錄 * @return boolean 檢測結(jié)果,true-通過,false-失敗 */ public function checkSavePath($savepath){ /* 檢測并創(chuàng)建目錄 */ if (!$this->mkdir($savepath )) { return false; } else { /* 檢測目錄是否可寫 */ if (!is_writable($savepath)) { $this->error = '上傳目錄不可寫!'; return false; } else { return true; } } } /** * 檢查上傳的文件 * @param array $file 文件信息 */ private function check($file) { /* 檢查文件大小 */ if (!$this->checkSize($file->getSize())) { $this->error = '上傳文件大小不符!'; return false; } /* 檢查文件后綴 */ if (!$this->checkExt($file->ext)) { $this->error = '上傳文件后綴不允許'; return false; } /* 通過檢測 */ return true; } /** * 檢查文件大小是否合法 * @param integer $size 數(shù)據(jù) */ private function checkSize($size) { return !($size > $this->maxSize) || (0 == $this->maxSize); } /** * 檢查上傳的文件后綴是否合法 * @param string $ext 后綴 */ private function checkExt($ext) { return emptyempty($this->config['exts']) ? true : in_array(strtolower($ext), $this->exts); } /** * 創(chuàng)建目錄 * @param string $savepath 要創(chuàng)建的穆里 * @return boolean 創(chuàng)建狀態(tài),true-成功,false-失敗 */ protected function mkdir($savepath){ if(is_dir($savepath)){ return true; } if(mkdir($savepath, 0777, true)){ return true; } else { $this->error = "目錄創(chuàng)建失敗"; 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);
總結(jié)
以上所述是小編給大家介紹的PHP Laravel 上傳圖片、文件等類封裝,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對綠夏網(wǎng)網(wǎng)站的支持!
#免責(zé)聲明#
本站[綠夏技術(shù)導(dǎo)航]提供的一切軟件、教程和內(nèi)容信息僅限用于學(xué)習(xí)和研究目的;不得將上述內(nèi)容用于商業(yè)或者非法用途,否則,一切后果請用戶自負(fù)。本站信息來自網(wǎng)絡(luò)收集整理,版權(quán)爭議與本站無關(guān)。您必須在下載后的24個(gè)小時(shí)之內(nèi),從您的電腦或手機(jī)中徹底刪除上述內(nèi)容。如果您喜歡該程序或內(nèi)容,請支持正版,購買注冊,得到更好的正版服務(wù)。我們非常重視版權(quán)問題,如有侵權(quán)請郵件[admin@lxwl520.com]與我們聯(lián)系進(jìn)行刪除處理。敬請諒解!