php实现文件上传
2018-02-03 10:37
162 查看
存档:
upload1.html(单文件上传)
upload2.html(多文件上传)
upload.php
fileupload.class.php
结果如下:
upload1.html(单文件上传)
<html> <head> <title>单个文件上传</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 选择文件:<input type="file" name="myfile"> <input type="submit" value="上传文件"> </form> </body> </html>
upload2.html(多文件上传)
<html> <head> <title>多文件上传</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 选择文件1:<input type="file" name="myfile[]"><br> 选择文件2:<input type="file" name="myfile[]"><br> 选择文件3:<input type="file" name="myfile[]"><br> <input type="submit" value="上传文件"> </form> </body> </html>
upload.php
<?php require "fileupload.class.php"; $up=new FileUpload; $up ->set('path','./newpath/') ->set('size',1000000) ->set('allowtype',array('gif','jpg','png')) ->set('israndname',false); if($up->upload('myfile')){ print_r($up->getFileName()); } else{ print_r($up->getErrorMsg()); } ?>
fileupload.class.php
<?php class FileUpload{ private $path="./uploads"; private $allowtype=array('jpg','gif','png'); private $maxsize=1000000; private $israndname=true; private $originName; private $tmpFileName; private $fileType; private $fileSize; private $newFileName; private $errorNum=0; private $errorMess=""; function set($key,$val){ $key=strtolower($key); if(array_key_exists($key,get_class_vars(get_class($this)))){ $this->setOption($key,$val); } return $this; } function upload($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{ $errors[]=$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; } $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; } } public function getFileName(){ return $this->newFileName; } public function getErrorMsg(){ return $this->errorMess; } 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}个字节"; break; case -3: $str.="上传失败"; break; case -4: $str.="建立存放上传文件目录失败,请重新指定上传目录"; break; case -5: $str.="必须指定上传文件的路径"; break; default: $str.="未知错误"; //break; } return $str.'<br>'; } 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); $aryStr=explode(".",$name); $this->setOption('fileType',strtolower($aryStr[count($aryStr)-1])); $this->setOption('fileSize',$size); return true; } private function setOption($key,$val){ $this->$key=$val; } private function setNewFileName(){ if($this->israndname){ $this->setOption('newFileName',$this->proRandName()); } else{ $this->setOption('newFileName',$this->originName); } } private function checkFileType(){ if(in_array(strtolower($this->fileType),$this->allowtype)){ return true; } else{ $this->setOption('errorNum',-1); return false; } } private function checkFileSize(){ if($this->fileSize>$this->maxsize){ $this->setOption('errorNum',-2); return false; } else{ return true; } } private function checkFilePath(){ if(empty($this->path)){ $this->setOption('errorNum',-5); return false; } if(!file_exists($this->path)||!is_writable($this->path)){ if(!@mkdir($this->path,0755)){ $this->setOption('errorNum',-4); return false; } } return true; } private function proRandName(){ $fileName = date('YmdHis')."_".rand(100,999); return $fileName.'.'.$this->fileType; } private function copyFile(){ if(!$this->errorNum){ $path=rtrim($this->path,'/').'/'; $path.=$this->newFileName; if(@move_uploaded_file($this->tmpFileName,$path)){ return true; } else{ $this->setOption('errorNum',-3); return false; } } else{ return false; } } } ?>
结果如下:
相关文章推荐
- 用PHP实现文件上传
- 用PHP文件上传的具体思路及实现
- PHP+FLASH实现上传文件进度条相关文件 下载
- 文件上传的实现 (马马 2001年09月28日 17:14) PHP同样支持上传文件的功能,但并不是所有支持PHP的网站都支持此功能,特别是免费的网站。 实现上传,首先要在HTML上加入上传文件的“<FORM>”表格。 <form me
- 用PHP实现文件上传
- 用PHP实现文件上传
- PHP 文件上传进度条的两种实现方法的代码
- PHP文件上传实现的注意点
- PHP上传文件进度条的实现详情
- PHP 大文件上传进度条实现
- SIMPLE PHP -- 实现文件上传
- php实现文件上传的一些经验
- PHP文件上传的原理及实现
- 用PHP实现文件上传
- PHP文件上传实现的注意点
- 基于PHP的AJAX技术实现文件异步上传
- PHP 大文件上传进度条实现
- 用PHP实现文件上传二法
- 用PHP实现文件上传
- PHP实现文件上传例子