php文件上傳類的分享
本文實(shí)例為大家分享了php文件上傳類的具體代碼,供大家參考,具體內(nèi)容如下
<?php
$upload = new UpLoad();
$upload->uploadFile('fm');
/*打印錯(cuò)誤信息*/
// var_dump($upload->errorNumber);
// var_dump($upload->errorInfo);
class UpLoad{
//文件上傳路徑
protected $path = 'upload/';
//允許文件上傳的后綴
protected $allowSuffix = ['jpg','jpeg',
'gif','wbmp','png'];
//mime類型
protected $allowMime =['image/jpg','image/jpeg',
'image/gif','image/wbmp','image/png'];
//允許上傳的大小
protected $maxSize = 2000000;
//是否啟用默認(rèn)的前綴
protected $isRandName =true ;
//文件的前綴
protected $prefix = 'up_';
//錯(cuò)誤號(hào)和錯(cuò)誤信息
protected $errorNumber;
protected $errorInfo;
//文件的信息
//文件名
protected $oldName;
//文件的后綴
protected $suffix;
//文件的大小
protected $size;
//文件的mime
protected $mime;
//文件的臨時(shí)文件的路徑
protected $tmpName;
//文件新名字
protected $newName;
//構(gòu)造方法
//因?yàn)槌蓡T屬性比較多就用數(shù)組來顯示
public function __construct($arr =[]){
foreach ($arr as $key=>$value){
$this->setOption($key,$value);
}
}
//判斷$key是不是我的成員屬性,如果是就設(shè)置
protected function setOption($key,$value){
//得到所有的成員屬性
$keys = array_keys(get_class_vars(__CLASS__));
if(in_array($key, $keys)){
$this->$key = $value;
}
}
//文件上傳函數(shù)
//key 就是input框中的name屬性值
public function uploadFile($key){
//判斷有沒有設(shè)置路徑 path
if(empty($this->path)){
$this->setOption('errorNumber',-1 );
return false;
}
//判斷該路徑是否存在是否可寫
if (!$this->check()){
$this->setOption('errorNumber', -2);
return false;
}
//判斷$_FILES里面的error信息是否為0,如果為0則說明文件信息在服務(wù)器端可以直接獲取,提取信息保存到成員屬性中
$error = $_FILES[$key]['error'];
if($error){
$this->setOption('errorNumber', -3);
return false;
}else {
//提取文件相關(guān)信息并且保存到成員屬性中
$this->getFileInfo($key);
}
//判斷文件的大小、mime、后綴是否符合
if(!$this->checkSize() || !$this->checkMime()|| !$this->checkSuffix()){
return false;
}
//得到新的文件名字
$this->newName = $this->createNewName();
//判斷是否是上傳文件,并且是移動(dòng)上傳文件
if(is_uploaded_file($this->tmpName)){
if(move_uploaded_file($this->tmpName, $this->path.$this->newName)){
return $this->path.$this->newName;
}else {
$this->setOption('errorNumber', -7);
return false;
}
}else{
$this->setOption('errorNumber', -6);
return false;
}
}
//檢測(cè)文件夾是否存在,是否可寫
protected function check(){
//文件夾不存在或者不是目錄。創(chuàng)建文件夾
if(!file_exists($this->path) ||!is_dir($this->path)){
return mkdir($this->path,0777,true);
}
//判斷文件是否可寫
if(!is_writeable($this->path)){
return chmod($this->path, 0777);
}
return true;
}
//根據(jù)key得到文件信息
protected function getFileInfo($key){
//得到文件的名字
$this->oldName = $_FILES[$key]['name'];
//得到文件的mime類型
$this->mime = $_FILES[$key]['type'];
//得到文件的臨時(shí)文件
$this->tmpName = $_FILES[$key]['tmp_name'];
//得到文件大小
$this->size = $_FILES[$key]['size'];
//得到文件后綴
$this->suffix = pathinfo($this->oldName)['extension'];
}
//判斷文件大小
protected function checkSize(){
if($this->size > $this->maxSize){
$this->setOption('errorNumber', -3);
return false;
}
return true;
}
//判斷mime類型
protected function checkMime(){
if(!in_array($this->mime, $this->allowMime)){
$this->setOption('errorNumber', -4);
return false;
}
return true;
}
//判斷后綴
protected function checkSuffix(){
if(!in_array($this->suffix, $this->allowSuffix)){
$this->setOption('errorNumber', -5);
return false;
}
return true;
}
//創(chuàng)建新名字
protected function createNewName(){
if($this->isRandName){
$name = $this->prefix.uniqid().'.'.$this->suffix;
}else {
$name = $this->prefix.$this->oldName;
}
return $name;
}
public function __get($name){
if($name == 'errorNumber'){
return $this->errorNumber;
}elseif ($name == 'errorInfo'){
return $this->getErrorInfo();
}
}
protected function getErrorInfo(){
switch ($this->errorNumber){
case -1:
$str = '文件路徑?jīng)]有設(shè)置';
break;
case -2:
$str = '文件不是目錄或者不可寫';
break;
case -3:
$str = '文件超過指定大小';
break;
case -4:
$str = 'mime類型不符合';
break;
case -5:
$str = '文件后綴不符合';
break;
case -6:
$str = '不是上傳文件';
break;
case -7:
$str = '移動(dòng)失敗';
break;
case 1:
$str = '超出ini設(shè)置大小';
break;
case 2:
$str = '超出html表單大小';
break;
case 3:
$str = '文章只有部分上傳';
break;
case 4:
$str = '沒有文件上傳';
break;
case 6:
$str = '找不到臨時(shí)文件';
break;
case 7:
$str = '文件寫入失敗';
break;
}
return $str;
}
}
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>文件上傳</title> </head> <body> <form action="UpLoad.php" method="post" enctype="multipart/form-data" > <input type="file" name="fm" value=""><br> <input type="submit" value="上傳文件" /><br> </form> </body> </html>
注意:input中的name必須和上傳類中的uploadFile中是傳值一致!
更多精彩內(nèi)容請(qǐng)參考php文件上傳操作匯總進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
判斷、添加和刪除WordPress置頂文章的相關(guān)PHP函數(shù)小結(jié)
這篇文章主要介紹了判斷、添加和刪除WordPress置頂文章的相關(guān)PHP函數(shù)小結(jié),需要的朋友可以參考下2015-12-12
PHP實(shí)現(xiàn)獲取FLV文件的時(shí)間
這篇文章主要介紹了PHP實(shí)現(xiàn)獲取FLV文件的時(shí)間,本文直接給出實(shí)現(xiàn)代碼和使用方法,需要的朋友可以參考下2015-02-02
淺談laravel orm 中的一對(duì)多關(guān)系 hasMany
今天小編就為大家分享一篇淺談laravel orm 中的一對(duì)多關(guān)系 hasMany,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php注冊(cè)和登錄界面的實(shí)現(xiàn)案例(推薦)
下面小編就為大家?guī)硪黄猵hp注冊(cè)和登錄界面的實(shí)現(xiàn)案例(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
tp框架(thinkPHP)實(shí)現(xiàn)三次登陸密碼錯(cuò)誤之后鎖定賬號(hào)功能示例
這篇文章主要介紹了tp框架(thinkPHP)實(shí)現(xiàn)三次登陸密碼錯(cuò)誤之后鎖定賬號(hào)功能,結(jié)合實(shí)例形式分析了基于thinkPHP登陸判斷、標(biāo)志位運(yùn)算等操作實(shí)現(xiàn)密碼賬號(hào)鎖定功能,需要的朋友可以參考下2018-05-05

