PHP上传文件处理类(实例)

HTML

<code>/<code>


PHP

<code>require "FileUpload.class.php";
\t$up=new FileUpload(array('isRandName'=>true,'allowType'=>array('txt', 'doc', 'php', 'gif'),'FilePath'=>'./uploads/', 'MAXSIZE'=>200000));
\techo '
';
\tif($up->uploadFile('spic')){
\t\tprint_r($up->getNewFileName());
\t}else{
\t\tprint_r($up->getErrorMsg());\t
\t}
\techo '
';/<code>


Class 类文件

<code>\tclass FileUpload {
\t\tprivate $filepath; //指定上传文件保存的路径
\t\tprivate $allowtype=array('gif', 'jpg', 'png', 'jpeg'); //充许上传文件的类型
\t\tprivate $maxsize=1000000; //允上传文件的最大长度 1M
\t\tprivate $israndname=true; //是否随机重命名, true false不随机,使用原文件名
\t\tprivate $originName; //源文件名称
\t\tprivate $tmpFileName; //临时文件名
\t\tprivate $fileType; //文件类型
\t\tprivate $fileSize; //文件大小
\t\tprivate $newFileName; //新文件名
\t\tprivate $errorNum=0; //错误号
\t\tprivate $errorMess=""; //用来提供错误报告
\t\t//用于对上传文件初使化
\t\t//1. 指定上传路径, 2,充许的类型, 3,限制大小, 4,是否使用随机文件名称

\t\t//让用户可以不用按位置传参数,后面参数给值不用将前几个参数也提供值
\t\tfunction __construct($options=array()){
\t\t\tforeach($options as $key=>$val){
\t\t\t\t$key=strtolower($key);
\t\t\t\t//查看用户参数中数组的下标是否和成员属性名相同
\t\t\t\tif(!in_array($key,get_class_vars(get_class($this)))){
\t\t\t\t\tcontinue;
\t\t\t\t}
\t\t\t\t$this->setOption($key, $val);
\t\t\t}
\t\t
\t\t
\t\t}
\t
\t\tprivate function getError(){
\t\t\t$str="上传文件{$this->originName}/<font>时出错:";
\t\t\tswitch($this->errorNum){
\t\t\t\tcase 4: $str .= "没有文件被上传"; break;
\t\t\t\tcase 3: $str .= "文件只被部分上传"; break;
\t\t\t\tcase 2: $str .= "上传文件超过了HTML表单中MAX_FILE_SIZE选项指定的值"; break;
\t\t\t\tcase 1: $str .= "上传文件超过了php.ini 中upload_max_filesize选项的值"; break;
\t\t\t\tcase -1: $str .= "末充许的类型"; break;
\t\t\t\tcase -2: $str .= "文件过大,上传文件不能超过{$this->maxSize}个字节"; break;
\t\t\t\tcase -3: $str .= "上传失败"; break;
\t\t\t\tcase -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;
\t\t\t\tcase -5: $str .= "必须指定上传文件的路径"; break;
\t\t\t\tdefault: $str .= "末知错误";
\t\t\t}
\t\t\treturn $str.'
';
\t\t}
\t
\t\t//用来检查文件上传路径

\t\tprivate function checkFilePath(){
\t\t\tif(empty($this->filepath)) {
\t\t\t\t$this->setOption('errorNum', -5);
\t\t\t\treturn false;
\t\t\t}
\t\t\tif(!file_exists($this->filepath) || !is_writable($this->filepath)){
\t\t\t\tif(!@mkdir($this->filepath, 0755)){
\t\t\t\t\t$this->setOption('errorNum', -4);
\t\t\t\t\treturn false;
\t\t\t\t}
\t\t\t}
\t\t\treturn true;
\t\t}
\t\t//用来检查文件上传的大小
\t\tprivate function checkFileSize() {
\t\t\tif($this->fileSize > $this->maxsize){
\t\t\t\t$this->setOPtion('errorNum', '-2');
\t\t\t\treturn false;
\t\t\t}else{
\t\t\t\treturn true;
\t\t\t}
\t\t}
\t\t//用于检查文件上传类型
\t\tprivate function checkFileType() {
\t\t\tif(in_array(strtolower($this->fileType), $this->allowtype)) {
\t\t\t\treturn true;
\t\t\t}else{
\t\t\t\t$this->setOption('errorNum', -1);
\t\t\t\treturn false;
\t\t\t}
\t\t}
\t\t//设置上传后的文件名称
\t\tprivate function setNewFileName(){
\t\t\tif($this->israndname){
\t\t\t\t$this->setOption('newFileName', $this->proRandName());
\t\t\t} else {
\t\t\t\t$this->setOption('newFileName', $this->originName);
\t\t\t}
\t\t}
\t\t//设置随机文件名称
\t\tprivate function proRandName(){
\t\t\t$fileName=date("YmdHis").rand(100,999);
\t\t\treturn $fileName.'.'.$this->fileType;
\t\t}
\t
\t\tprivate function setOption($key, $val){
\t\t\t$this->$key=$val;

\t\t}
\t\t//用来上传一个文件
\t\tfunction uploadFile($fileField){
\t\t\t$return=true;
\t\t\t//检查文件上传路径
\t\t\tif(!$this->checkFilePath()){
\t\t\t\t$this->errorMess=$this->getError();
\t\t\t\treturn false;
\t\t\t}
\t\t\t
\t\t\t$name=$_FILES[$fileField]['name'];
\t\t\t$tmp_name=$_FILES[$fileField]['tmp_name'];
\t\t\t$size=$_FILES[$fileField]['size'];
\t\t\t$error=$_FILES[$fileField]['error'];
\t\t\tif(is_Array($name)){
\t\t\t\t$errors=array();
\t\t\t\tfor($i=0; $i<count>\t\t\t\t\tif($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
\t\t\t\t\t\tif(!$this->checkFileSize() || !$this->checkFileType()){
\t\t\t\t\t\t\t$errors[]=$this->getError();
\t\t\t\t\t\t\t$return=false;
\t\t\t\t\t\t}
\t\t\t\t\t}else{
\t\t\t\t\t\t$error[]=$this->getError();
\t\t\t\t\t\t$return=false;
\t\t\t\t\t}
\t\t\t\t\tif(!$return)
\t\t\t\t\t\t$this->setFiles();
\t\t\t\t}
\t\t\t\tif($return){
\t\t\t\t\t$fileNames=array();
\t\t\t\t\tfor($i=0; $i<count>\t\t\t\t\t\tif($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){
\t\t\t\t\t\t\t$this->setNewFileName();
\t\t\t\t\t\t\tif(!$this->copyFile()){
\t\t\t\t\t\t\t\t$errors=$this->getError();
\t\t\t\t\t\t\t\t$return=false;
\t\t\t\t\t\t\t}else{
\t\t\t\t\t\t\t\t$fileNames[]=$this->newFileName;
\t\t\t\t\t\t\t}
\t\t\t\t\t\t}
\t\t\t\t\t}
\t\t\t\t\t$this->newFileName=$fileNames;
\t\t\t\t}
\t\t\t\t$this->errorMess=$errors;
\t\t\t\treturn $return;
\t\t\t} else {
\t\t\t\t
\t\t\t\t\tif($this->setFiles($name, $tmp_name, $size, $error)){
\t\t\t\t\t\tif($this->checkFileSize() && $this->checkFileType()){
\t\t\t\t\t\t\t$this->setNewFileName();

\t\t\t\t\t\t\tif($this->copyFile()){
\t\t\t\t\t\t\t\treturn true;
\t\t\t\t\t\t\t}else{
\t\t\t\t\t\t\t\t$return=false;
\t\t\t\t\t\t\t}
\t\t\t\t\t\t\t\t
\t\t\t\t\t\t}else{
\t\t\t\t\t\t\t$return=false;
\t\t\t\t\t\t}\t
\t\t\t\t\t}else{
\t\t\t\t\t\t$return=false;
\t\t\t\t\t}
\t\t\t\t\t
\t\t\t\t\t
\t\t\t\t\tif(!$return)
\t\t\t\t\t\t$this->errorMess=$this->getError();
\t\t\t\t\treturn $return;
\t\t\t}\t\t\t
\t\t}
\t\tprivate function copyFile(){
\t\t\tif(!$this->errorNum){
\t\t\t\t$filepath=rtrim($this->filepath, '/').'/';
\t\t\t\t$filepath.=$this->newFileName;
\t\t\t\tif(@move_uploaded_file($this->tmpFileName, $filepath))\t{
\t\t\t\t\treturn true;
\t\t\t\t}else{
\t\t\t\t\t$this->setOption('errorNum', -3);
\t\t\t\t\treturn false;
\t\t\t\t}
\t\t\t\t\t
\t\t\t}else{
\t\t\t\treturn false;
\t\t\t}
\t\t}
\t\t//设置和$_FILES有关的内容
\t\tprivate function setFiles($name="", $tmp_name='', $size=0, $error=0){
\t\t
\t\t\t$this->setOption('errorNum', $error);
\t\t\t\t
\t\t\tif($error){
\t\t\t\treturn false;
\t\t\t}
\t\t\t$this->setOption('originName', $name);
\t\t\t$this->setOption('tmpFileName', $tmp_name);
\t\t\t$arrStr=explode('.', $name);
\t\t\t$this->setOption('fileType', strtolower($arrStr[count($arrStr)-1]));
\t\t\t$this->setOption('fileSize', $size);\t
\t\t\treturn true;
\t\t}\t
\t\t//用于获取上传后文件的文件名

\t\tfunction getNewFileName(){
\t\t\treturn $this->newFileName;
\t\t}
\t\t//上传如果失败,则调用这个方法,就可以查看错误报告
\t\tfunction getErrorMsg() {
\t\t\treturn $this->errorMess;
\t\t}
\t}
/<count>/<count>/<code>


PHP上传文件处理类(实例)


分享到:


相關文章: