PHP Laravel 上傳圖片、文件等類封裝
更新時間:2017年08月16日 14:19:12 作者:B5教程網(wǎng)
這篇文章主要介紹了PHP Laravel 上傳圖片、文件等類封裝的實現(xiàn)代碼,需要的朋友可以參考下
今天把項目中上傳功能封裝成類,方便后面使用,簡單的封裝了一下,感覺還不怎么好,后面繼續(xù)優(yōu)化。
具體代碼如下:
<?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 = ''; //上傳錯誤信息
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ù),多個參數(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]);
}
/**
* 獲取最后一次上傳錯誤信息
* @return string 錯誤信息
*/
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 \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).'.'.$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 App\ThinkClass\UploadClass;
$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 上傳圖片、文件等類封裝,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:
- Laravel 5使用Laravel Excel實現(xiàn)Excel/CSV文件導入導出的功能詳解
- vuejs+element-ui+laravel5.4上傳文件的示例代碼
- 修改Laravel5.3中的路由文件與路徑
- 在Laravel5中正確設(shè)置文件權(quán)限的方法
- PHP框架laravel的.env文件配置教程
- Laravel最佳分割路由文件(routes.php)的方式
- PHP Laravel實現(xiàn)文件下載功能
- Laravel框架文件上傳功能實現(xiàn)方法示例
- Laravel基礎(chǔ)-關(guān)于引入公共文件的兩種方式
- 使用laravel指定日志文件記錄任意日志
- Laravel5.1 框架文件管理操作實例分析

