class Upload
{
//保存路徑
protected $savePath = './';
//日期目錄
protected $datePath = true;
//隨機名字
protected $randName = true;
//默認后綴
protected $extension = 'png';
//允許MIME
protected $mimes = ['image/png','image/jpeg','image/gif'];
//允許后綴
protected $suffixes = ['png','jpg','jpeg','gif'];
//最大尺寸
protected $maxSize = 2000000;
//錯誤代碼
protected $errorNumber = 0;
//錯誤信息
protected $errorMessage = '上傳成功';
//上傳信息
protected $uploadInfo;
//文件名稱
protected $pathName;
public function __construct($options=null)
{
$this->setOption($options);
}
著作權歸作者所有褒侧。商業(yè)轉載請聯(lián)系作者獲得授權分别,非商業(yè)轉載請注明出處∪恼蓿互聯(lián)網(wǎng)+時代兔乞,時刻要保持學習创坞,攜手千鋒PHP,Dream?It?Possible异吻。
public function setOption($options)
{
if (is_array($options)) {
$keys = get_class_vars(__CLASS__);
foreach ($options as $key => $value) {
//在屬性列表中才設置屬性
if (in_array($key, $keys)) {
$this->$key = $value;
}
}
}
}
public function uploadFile($field)
{
//1唱凯、檢查保存路徑
if (!$this->checkSavePath()) {
return false;
}
//2桨仿、檢查上傳信息
if (!$this->checkUploadInfo($field)) {
return false;
}
//3睛低、檢查系統(tǒng)錯誤
if (!$this->checkUploadError()) {
return false;
}
//4、檢查自定義錯誤
if (!$this->checkAllowOption()) {
return false;
}
//5、檢車是否是上傳文件
if (!$this->checkUploadFile()) {
return false;
}
//6钱雷、拼接路徑名
$this->joinPathName();
//7骂铁、移動上傳文件
if (!$this->moveUploadFile()) {
return false;
}
return true;
}
protected function checkSavePath()
{
if (!is_dir($this->savePath)) {
$this->errorNumber = -1;
$this->errorMessage = '保存路徑不存在';
return false;
}
if (!is_writable($this->savePath)) {
$this->errorNumber = -2;
$this->errorMessage = '保存路徑不可寫';
return false;
}
$this->savePath = rtrim($this->savePath,'/') . '/';
return true;
}
protected function checkUploadInfo($field)
{
if (empty($_FILES[$field])) {
$this->errorNumber = -3;
$this->errorMessage = '沒有'.$field.'相關上傳信息';
return false;
}
$this->uploadInfo = $_FILES[$field];
return true;
}
protected function checkUploadError()
{
if ($this->uploadInfo['error'] == 0) {
return true;
}
switch ($this->uploadInfo['error']) {
case UPLOAD_ERR_INI_SIZE:
$this->errorMessage = '超出了配置文件中設定文件大小';
break;
case UPLOAD_ERR_FORM_SIZE:
$this->errorMessage = '超出了MAX_FILE_SIZE的大小';
break;
case UPLOAD_ERR_PARTIAL:
$this->errorMessage = '只有部分文件被上傳';
break;
case UPLOAD_ERR_NO_FILE:
$this->errorMessage = '沒有文件被上傳';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$this->errorMessage = '沒有找到臨時文件夾';
break;
case UPLOAD_ERR_CANT_WRITE:
$this->errorMessage = '文件寫入失敗';
break;
default:
$this->errorMessage = '未知錯誤';
break;
}
$this->errorNumber = $this->uploadInfo['error'];
return false;
}
protected function checkAllowOption()
{
if (!in_array($this->uploadInfo['type'],$this->mimes)) {
$this->errorNumber = -4;
$this->errorMessage = '不允許的MIME:'.$this->uploadInfo['type'];
return false;
}
if (!in_array($this->extension,$this->suffixes)) {
$this->errorNumber = -5;
$this->errorMessage = '不允許的后綴:'.$this->extension;
return false;
}
if ($this->uploadInfo['size'] > $this->maxSize) {
$this->errorNumber = -6;
$this->errorMessage = '超出了規(guī)定大小:'.$this->maxSize.'字節(jié)';
return false;
}
return true;
}
protected function checkUploadFile()
{
if (!is_uploaded_file($this->uploadInfo['tmp_name'])) {
$this->errorNumber = -7;
$this->errorMessage = '不是上傳文件';
return false;
}
return true;
}
protected function joinPathName()
{
//路徑
$this->pathName = $this->savePath;
if ($this->datePath) {
$this->pathName .= date('Y/m/d/');
if (!file_exists($this->pathName)) {
mkdir($this->pathName,0777,true);
}
}
//名字
if ($this->randName) {
$this->pathName .= uniqid();
} else {
$info = pathinfo($this->uploadInfo['name']);
$this->pathName .= $info['filename'];
}
//后綴
$this->pathName .= '.' . $this->extension;
}
protected function moveUploadFile()
{
if (move_uploaded_file($this->uploadInfo['tmp_name'], $this->pathName)) {
return true;
}
$this->errorNumber = -8;
$this->errorMessage = '上傳文件保存';
return false;
}
}
更多PHP相關技術請搜索千鋒PHP,做真實的自己急波,用良心做教育