日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

fileupload.class.php,php文件上传类

發(fā)布時(shí)間:2025/3/21 php 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 fileupload.class.php,php文件上传类 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

php文件上傳類

FileUpload.class.php

class?FileUpload?{

private?$filepath;?????//指定上傳文件保存的路徑

private?$allowtype=array('gif',?'jpg',?'png',?'jpeg');??//充許上傳文件的類型

private?$maxsize=1000000;??//允上傳文件的最大長(zhǎng)度?1M

private?$israndname=true;??//是否隨機(jī)重命名,?true?false不隨機(jī),使用原文件名

private?$originName;???//源文件名稱

private?$tmpFileName;???//臨時(shí)文件名

private?$fileType;??//文件類型

private?$fileSize;??//文件大小

private?$newFileName;?//新文件名

private?$errorNum=0;??//錯(cuò)誤號(hào)

private?$errorMess="";?//用來(lái)提供錯(cuò)誤報(bào)告

//用于對(duì)上傳文件初使化

//1.?指定上傳路徑,?2,充許的類型,?3,限制大小,?4,是否使用隨機(jī)文件名稱

//讓用戶可以不用按位置傳參數(shù),后面參數(shù)給值不用將前幾個(gè)參數(shù)也提供值

function?__construct($options=array()){

foreach($options?as?$key=>$val){

$key=strtolower($key);

//查看用戶參數(shù)中數(shù)組的下標(biāo)是否和成員屬性名相同

if(!in_array($key,get_class_vars(get_class($this)))){

continue;

}

$this->setOption($key,?$val);

}

}

private?function?getError(){

$str="上傳文件{$this->originName}時(shí)出錯(cuò):";

switch($this->errorNum){

case?4:?$str?.=?"沒(méi)有文件被上傳";?break;

case?3:?$str?.=?"文件只被部分上傳";?break;

case?2:?$str?.=?"上傳文件超過(guò)了HTML表單中MAX_FILE_SIZE選項(xiàng)指定的值";?break;

case?1:?$str?.=?"上傳文件超過(guò)了php.ini?中upload_max_filesize選項(xiàng)的值";?break;

case?-1:?$str?.=?"末充許的類型";?break;

case?-2:?$str?.=?"文件過(guò)大,上傳文件不能超過(guò){$this->maxSize}個(gè)字節(jié)";?break;

case?-3:?$str?.=?"上傳失敗";?break;

case?-4:?$str?.=?"建立存放上傳文件目錄失敗,請(qǐng)重新指定上傳目錄";?break;

case?-5:?$str?.=?"必須指定上傳文件的路徑";?break;

default:?$str?.=??"末知錯(cuò)誤";

}

return?$str.'
';

}

//用來(lái)檢查文件上傳路徑

private?function?checkFilePath(){

if(empty($this->filepath))?{

$this->setOption('errorNum',?-5);

return?false;

}

if(!file_exists($this->filepath)?||?!is_writable($this->filepath)){

if(!@mkdir($this->filepath,?0755)){

$this->setOption('errorNum',?-4);

return?false;

}

}

return?true;

}

//用來(lái)檢查文件上傳的大小

private?function?checkFileSize()?{

if($this->fileSize?>?$this->maxsize){

$this->setOPtion('errorNum',?'-2');

return?false;

}else{

return?true;

}

}

//用于檢查文件上傳類型

private?function?checkFileType()?{

if(in_array(strtolower($this->fileType),?$this->allowtype))?{

return?true;

}else{

$this->setOption('errorNum',?-1);

return?false;

}

}

//設(shè)置上傳后的文件名稱

private?function?setNewFileName(){

if($this->israndname){

$this->setOption('newFileName',?$this->proRandName());

}?else?{

$this->setOption('newFileName',?$this->originName);

}

}

//設(shè)置隨機(jī)文件名稱

private?function?proRandName(){

$fileName=date("YmdHis").rand(100,999);

return?$fileName.'.'.$this->fileType;

}

private?function?setOption($key,?$val){

$this->$key=$val;

}

//用來(lái)上傳一個(gè)文件

function?uploadFile($fileField){

$return=true;

//檢查文件上傳路徑

if(!$this->checkFilePath()){

$this->errorMess=$this->getError();

return?false;

}

$name=$_FILES[$fileField]['name'];

$tmp_name=$_FILES[$fileField]['tmp_name'];

$size=$_FILES[$fileField]['size'];

$error=$_FILES[$fileField]['error'];

if(is_Array($name)){

$errors=array();

for($i=0;?$i

if($this->setFiles($name[$i],?$tmp_name[$i],?$size[$i],?$error[$i])){

if(!$this->checkFileSize()?||?!$this->checkFileType()){

$errors[]=$this->getError();

$return=false;

}

}else{

$error[]=$this->getError();

$return=false;

}

if(!$return)

$this->setFiles();

}

if($return){

$fileNames=array();

for($i=0;?$i

if($this->setFiles($name[$i],?$tmp_name[$i],?$size[$i],?$error[$i])){

$this->setNewFileName();

if(!$this->copyFile()){

$errors=$this->getError();

$return=false;

}else{

$fileNames[]=$this->newFileName;

}

}

}

$this->newFileName=$fileNames;

}

$this->errorMess=$errors;

return?$return;

}?else?{

if($this->setFiles($name,?$tmp_name,?$size,?$error)){

if($this->checkFileSize()?&&?$this->checkFileType()){

$this->setNewFileName();

if($this->copyFile()){

return?true;

}else{

$return=false;

}

}else{

$return=false;

}

}else{

$return=false;

}

if(!$return)

$this->errorMess=$this->getError();

return?$return;

}

}

private?function?copyFile(){

if(!$this->errorNum){

$filepath=rtrim($this->filepath,?'/').'/';

$filepath.=$this->newFileName;

if(@move_uploaded_file($this->tmpFileName,?$filepath))????{

return?true;

}else{

$this->setOption('errorNum',?-3);

return?false;

}

}else{

return?false;

}

}

//設(shè)置和$_FILES有關(guān)的內(nèi)容

private?function?setFiles($name="",?$tmp_name='',?$size=0,?$error=0){

$this->setOption('errorNum',?$error);

if($error){

return?false;

}

$this->setOption('originName',?$name);

$this->setOption('tmpFileName',?$tmp_name);

$arrStr=explode('.',?$name);

$this->setOption('fileType',?strtolower($arrStr[count($arrStr)-1]));

$this->setOption('fileSize',?$size);

return?true;

}

//用于獲取上傳后文件的文件名

function?getNewFileName(){

return?$this->newFileName;

}

//上傳如果失敗,則調(diào)用這個(gè)方法,就可以查看錯(cuò)誤報(bào)告

function?getErrorMsg()?{

return?$this->errorMess;

}

}

uplode.php

require?"FileUpload.class.php";

$up=new?FileUpload(array('isRandName'=>true,'allowType'=>array('txt',?'doc',?'php',?'gif'),'FilePath'=>'./uploads/',?'MAXSIZE'=>200000));

echo?'

';

if($up->uploadFile('spic')){

print_r($up->getNewFileName());

}else{

print_r($up->getErrorMsg());

}

echo?'';

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的fileupload.class.php,php文件上传类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。