php檔案上傳類,支援產生檔案縮圖
阿新 • • 發佈:2019-01-09
個人寫的php檔案上傳類,支援所有檔案上傳,對於圖片型別的檔案,提供產生縮圖方法,使用者可以自己進行配置。寫的很一般,沒有太多的測試,目前是沒什麼bug,就當提供學習和參考吧,程式碼:
<?php
/**
* +----------------------------------------------------------------------
* | 檔案上傳類 Author Ftd Date: 15-03-18
* | 如果上傳的檔案是圖片型別,提供生成縮圖方法
* +----------------------------------------------------------------------
* | 主要方法:upload();createThumb();
* +----------------------------------------------------------------------
* | 使用方法: 建立一個config = array();
* | 提供引數:
* savePath string 可選 檔案上傳路徑(若為空則為當前目錄下的upload資料夾)
* thumbSavePath string 可選 縮圖上傳路徑(若為空則代表和savePath相同的路徑)
* ext string(逗號分割) 可選 允許上傳的檔案型別(若為空則代表允許上傳任何型別的檔案)
* maxSize int 可選 允許上傳的最大值(若為空或為0則表示無限大),此處和php.ini配置檔案無關
* fileName string 可選 上傳檔名(若為空則使用原始檔名)
* needThumb bool 可選 是否需要生成縮圖
* thumbWidth int 可選 縮圖寬度(若needThumb為true,則為必需)
* thumbHeight int 可選 縮圖高度(若needThumb為true,則為必需)
* needReplcePic bool 可選 是否覆蓋原始檔(若為空則表示不覆蓋,若savePath不等於thumbSavePath,此引數失效)
* +----------------------------------------------------------------------
* | 舉例:
*
* $config = array( //設定配置引數
* key => value
* )
* $obj = new UploadFile($config); //例項化物件
* $obj->upload($_FILES); //上傳檔案
* $obj->createThumb(); //生成縮圖
* +----------------------------------------------------------------------
*
*/
class UploadFile
{
private $savePath ; //上傳路徑
private $ext = array(); //允許的字尾名,為空則表示不限制
private $fileName ; //檔名
private $fileTempName;//臨時檔名
private $maxSize; //允許上傳最大值
private $fileExt;//檔案字尾名
private $fileSize;//檔案大小
private $errorNum; //錯誤號
private $errorMsg = ''; //錯誤資訊
private $srcFile; //原始檔,用來生成縮圖
private $thumbFile;//縮圖檔案
private $thumbSavePath;//縮圖上傳路徑
private $tempThumbImg;//臨時縮圖
private $thumbWidth; //縮圖寬度
private $thumbHeight; //縮圖高度
private $needThumb; //是否需要縮圖
private $needReplcePic;//是否覆蓋原始檔
//預設配置
private $acquiesce_config = array(
'savePath' => './upload/',
'thumbSavePath' => '',
'ext' => '',
'maxSize' => 0,
'fileName' => '',
'needThumb' => false,
'thumbWidth' => 0,
'thumbHeight' => 0,
'needReplcePic' => false
);
/**
* 構造方法
* @param [array] $config
*/
public function __construct($config = array())
{
//如果引數為完全設定,將其設定為預設引數
$config = array_merge($this->acquiesce_config,$config);
$this->set_config($config);
}
/**
* 設定上傳配置
* @param [array] $config
*/
private function set_config($config)
{
//分割字串得到檔案允許字尾名
$ext_arr = explode(',', $config['ext']);
foreach ($ext_arr as $k => $v) {
if($v != '')
{
$this->ext[] = $v;
}
}
$this->set('savePath',$config['savePath']);
$this->set('maxSize',$config['maxSize']);
$this->set('fileName',$config['fileName']);
$this->set('thumbHeight',$config['thumbHeight']);
$this->set('thumbWidth',$config['thumbWidth']);
$this->set('needThumb',$config['needThumb']);
$this->set('needReplcePic',$config['needReplcePic']);
$this->thumbSavePath = $config['thumbSavePath'] == '' ? $this->savePath : $config['thumbSavePath'];
}
/**
* [上傳檔案]
* @param [$file($_FILES)]
* @return [true|false]
*/
public function upload($file)
{
if(!empty($file))
{
$fileName = $file['file']['name'];
$fileSize = $file['file']['size'];
$fileTempName = $file['file']['tmp_name'];
$errorNum = $file['file']['error'];
$ext = $this->get_fileExt($fileName);
//檢查檔案引數
$flag = $this->check_file_and_config($fileSize,$ext,$errorNum);
if(!$flag)
{
return $this->get_errorMsg();
}
//設定檔名
if($this->fileName != null)
{
$fileName = $this->fileName .'.'. $ext;
}
//設定檔案屬性
$this->set_file($fileName,$fileTempName,$fileSize,$ext);
//移動(上傳)檔案
if(!$this->move_file())
{
return $this->get_errorMsg();
}
return 'success';
}
}
/**
* 得到檔案字尾名
* @param [string] $fileName [檔名]
* @return [string] [檔案字尾名]
*/
private function get_fileExt($fileName)
{
//得到檔案最後一個小數點,如果為false,說明檔案沒有後綴名
$is_has_ext = strripos($fileName, '.');
if(false == $is_has_ext)
{
return '';
}
$file_arr = explode('.', $fileName);
$fileExt = strtolower($file_arr[count($file_arr) - 1]);
return $fileExt;
}
/**
* 檢查檔案的合法性
* @param [int] $fileSize [檔案大小]
* @param [string] $ext [檔案字尾名]
* @param integer $errorNum [錯誤編號]
* @return [true|false]
*/
private function check_file_and_config($fileSize,$ext,$errorNum = 0)
{
$this->set('errorNum',$errorNum);
if($errorNum != 0)
{
return false;
}
if($this->savePath == '')
{
$this->set('errorNum',7);
return false;
}
if(!in_array($ext, $this->ext) && count($this->ext) != 0)
{
$this->set('errorNum',5);
return false;
}
if($fileSize > $this->maxSize && $this->maxSize != 0 && $this->maxSize != null)
{
$this->set('errorNum',6);
return false;
}
return true;
}
/**
* [設定成員變數]
* @param [string] $fileName [檔名]
* @param [string] $fileTempName [臨時檔名]
* @param [int] $fileSize [檔案大小]
* @param [string] $ext [檔案字尾名]
*/
private function set_file($fileName,$fileTempName,$fileSize,$ext)
{
$this->set('fileName',$fileName);
$this->set('fileTempName',$fileTempName);
$this->set('fileSize',$fileSize);
$this->set('fileExt',$ext);
}
/**
* [移動檔案]
* @return [true|false] [成功返回true]
*/
private function move_file()
{
if($this->errorNum == 0)
{
//建立檔案上傳目錄
$this->create_folders($this->savePath);
$move_file = $this->savePath .'/'. $this->fileName;
if(move_uploaded_file($this->fileTempName, $move_file))
{
//設定原始檔,用來建立縮圖
$this->srcFile = $move_file;
return true;
}else
{
$this->set('errorNum',8);
return false;
}
}else{
return false;
}
}
/**
* [生成縮圖]
* @return [string]
*/
public function createThumb()
{
$flag = $this->check_thumb_param();
if(!$flag)
{
return $this->get_errorMsg();
}
$this->build_temp_thumb_img();//建立臨時圖片
$this->build_thumb_path(); //建立縮圖路徑
if(!$this->build_thumb())
{
return $this->get_errorMsg();
}
return 'create_thumb_success';
}
/**
* [建立縮圖]
* @return [true|false]
*/
private function build_thumb()
{
//根據不同字尾名得到不同的方法
$image_create_method = $this->get_create_img_function();
//圖片實際寬度
$real_width = imagesx($this->tempThumbImg);
//圖片實際高度
$real_height = imagesy($this->tempThumbImg);
$thumb_img = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
$flag = imagecopyresampled($thumb_img, $this->tempThumbImg, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight, $real_width, $real_height);
$image_create_method($thumb_img,$this->thumbFile);
if(!$flag)
{
$this->set('errorNum',13);
return false;
}
return true;
}
/**
* [得到imagejpeg|imagegif|imagepng]
* @return [string] [方法名]
*/
private function get_create_img_function()
{
if($this->fileExt == 'jpg')
{
$method = 'image'.'jpeg';
return $method;
}
$method = 'image'.strtolower($this->fileExt);
return $method;
}
/**
* [檢查縮圖檔案引數]
* @return [true|false]
*/
private function check_thumb_param()
{
if($this->errorNum != 0)
{
return false;
}
if(empty($this->srcFile))
{
$this->set('errorNum',9);
return false;
}
if($this->fileExt != 'jpg' && $this->fileExt != 'gif' && $this->fileExt != 'png')
{
$this->set('errorNum',10);
return false;
}
if(!$this->needThumb)
{
$this->set('errorNum',11);
return false;
}
if($this->thumbWidth == 0 || $this->thumbHeight == 0 || $this->thumbWidth == '' || $this->thumbHeight == '')
{
$this->set('errorNum',12);
return false;
}
return true;
}
/**
* [建立縮圖路徑]
* @return [null]
*/
private function build_thumb_path()
{
if($this->savePath == $this->thumbSavePath)
{
if($this->needReplcePic)
{
$this->thumbFile = $this->srcFile;
}else
{
$this->thumbFile = $this->savePath.'/'.'thumb-'.$this->fileName;
}
}else{
$this->create_folders($this->thumbSavePath);
$this->thumbFile = $this->thumbSavePath.'/'.$this->fileName;
}
}
/**
* [建立臨時圖片]
* @return [null]
*/
private function build_temp_thumb_img()
{
switch ($this->fileExt) {
case 'jpg':
$this->set('tempThumbImg',imagecreatefromjpeg($this->srcFile));
break;
case 'gif':
$this->set('tempThumbImg',imagecreatefromgif($this->srcFile));
break;
case 'png':
$this->set('tempThumbImg',imagecreatefrompng($this->srcFile));
break;
default:
break;
}
}
/**
* 返回錯誤資訊
* @return [string]
*/
private function get_errorMsg()
{
switch ($this->errorNum) {
case 13:
$this->errorMsg = "生成縮圖失敗";
break;
case 12:
$this->errorMsg = "錯誤的縮率圖高度或寬度";
break;
case 11:
$this->errorMsg = "若要生成縮圖,請在config中設定needThumb為true";
break;
case 10:
$this->errorMsg = "該檔案不是圖片型別,不能產生縮圖";
break;
case 9:
$this->errorMsg = "原始檔不存在,無法生成縮圖,請先上傳檔案";
break;
case 8:
$this->errorMsg = "上傳失敗";
break;
case 7:
$this->errorMsg = '請設定上傳路徑';
break;
case 6:
$this->errorMsg = '上傳檔案大小超過maxSize最大值';
break;
case 5:
$this->errorMsg = "錯誤的檔案型別";
break;
case 4:
$this->errorMsg = '沒有檔案被上傳';
break;
case 3:
$this->errorMsg = '檔案只有部分被上傳';
break;
case 2:
$this->errorMsg = '上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值';
break;
case 1:
$this->errorMsg = '上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值';
break;
default:
break;
}
return $this->errorMsg;
}
/**
* set方法(過濾成員引數)
* @param $key
* @param $value
*/
public function set($key,$value)
{
//檢驗key是否是本類的成員屬性
if(array_key_exists($key, get_class_vars(get_class($this))))
{
$this->setProperty($key,$value);
}
}
/**
* [設定property]
* @param [property] $key
* @param [property] $value
*/
private function setProperty($key,$value)
{
if($key == 'ext')
{
$ext_arr = explode(',', $value);
foreach ($ext_arr as $k => $v) {
if($v != '')
{
$this->ext[] = $v;
}
}
return;
}
$this->$key = $value;
}
/**
* 遞迴建立資料夾
* @param [string] $dir [目錄]
* @return [true|false]
*/
private function create_folders($dir)
{
return is_dir($dir) or ($this->create_folders(dirname($dir)) and mkdir($dir, 0777));
}
}
?>
如有bug或建議,歡迎指正