PHP文件系统-文件上传类
生活随笔
收集整理的這篇文章主要介紹了
PHP文件系统-文件上传类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、文件上傳類的說明(這是一個可以處理單個文件上傳和多個文件上傳的處理類) 1、構(gòu)造函數(shù)初始化的作用是分配存儲空間和傳遞需要設(shè)定的參數(shù) 2、一個類的資源是有所有的定義變量的,不管是共有、私有的變量 二、該類的應(yīng)用方法和具體代碼 1、文件上傳的表單 <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="100000000"> <input type="file" name="spic[]"> <br> <input type="file" name="spic[]"> <br> <input type="file" name="spic[]"> <br> <input type="file" name="spic[]"> <br> <input type="submit" name="sub" value="upload file"><br> </form> 2、表單請求的文件上傳php頁面:upload.php <?php require "FileUpload.class.php"; $up=new FileUpload(array('isRandName'=>true,'allowType'=>array('txt', 'doc', 'php', 'gif'),'FilePath'=>'./uploads/', 'MAXSIZE'=>200000)); echo '<pre>'; if($up->uploadFile('spic')){ print_r($up->getNewFileName()); }else{ print_r($up->getErrorMsg()); } echo '</pre>'; ?> 3、上傳類的具體代碼:FileUpload.class.php <?php class FileUpload { private $filepath; //指定上傳文件保存的路徑 private $allowtype=array('gif', 'jpg', 'png', 'jpeg'); //充許上傳文件的類型 private $maxsize=1000000; //允上傳文件的最大長度 1M private $israndname=true; //是否隨機重命名, true false不隨機,使用原文件名 private $originName; //源文件名稱 private $tmpFileName; //臨時文件名 private $fileType; //文件類型 private $fileSize; //文件大小 private $newFileName; //新文件名 private $errorNum=0; //錯誤號 private $errorMess=""; //用來提供錯誤報告 ? //用于對上傳文件初使化 //1. 指定上傳路徑, 2,充許的類型, 3,限制大小, 4,是否使用隨機文件名稱 //讓用戶可以不用按位置傳參數(shù),后面參數(shù)給值不用將前幾個參數(shù)也提供值 function __construct($options=array()){ foreach($options as $key=>$val){ $key=strtolower($key); //查看用戶參數(shù)中數(shù)組的下標是否和成員屬性名相同 if(!in_array($key,get_class_vars(get_class($this)))){ continue; } $this->setOption($key, $val); } } private function getError(){ $str="上傳文件<font color='red'>{$this->originName}</font>時出錯:"; switch($this->errorNum){ case 4: $str .= "沒有文件被上傳"; break; case 3: $str .= "文件只被部分上傳"; break; case 2: $str .= "上傳文件超過了HTML表單中MAX_FILE_SIZE選項指定的值"; break; case 1: $str .= "上傳文件超過了php.ini 中upload_max_filesize選項的值"; break; case -1: $str .= "末充許的類型"; break; case -2: $str .= "文件過大,上傳文件不能超過{$this->maxSize}個字節(jié)"; break; case -3: $str .= "上傳失敗"; break; case -4: $str .= "建立存放上傳文件目錄失敗,請重新指定上傳目錄"; break; case -5: $str .= "必須指定上傳文件的路徑"; break; default: $str .= "末知錯誤"; } return $str.'<br>'; } //用來檢查文件上傳路徑 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; } //用來檢查文件上傳的大小 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è)置隨機文件名稱 private function proRandName(){ $fileName=date("YmdHis").rand(100,999); return $fileName.'.'.$this->fileType; } private function setOption($key, $val){ $this->$key=$val; } //用來上傳一個文件 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<count($name); $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<count($name); $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)用這個方法,就可以查看錯誤報告 function getErrorMsg() { return $this->errorMess; } } ?>
轉(zhuǎn)載于:https://www.cnblogs.com/gxldan/archive/2012/04/11/4066830.html
總結(jié)
以上是生活随笔為你收集整理的PHP文件系统-文件上传类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学生管理系统stuSystem函数
- 下一篇: js中动态引入css样式文件