A-A+

PHP实现的多文件上传类及用法示例

2019年07月31日 我爱编程 暂无评论

本文实例讲述了PHP实现的多文件上传类及用法。分享给大家供大家参考,具体如下:

1、upFiles.css.php 文件

  1. <?php
  2. class UploadFiles{
  3.  private $maxsize = '1000000'//允许上传文件最大长度
  4.  private $allowtype = array('jpg','png','gif','jpeg');//允许上传文件类型
  5.  private $israndfile = true;//是否随机文件名
  6.  private $filepath;//上传路径
  7.  private $originName;//上传的源文件
  8.  private $tmpfileName;//临时文件名
  9.  private $newfileName;//新文件名
  10.  private $fileSize;//文件大小
  11.  private $fileType;//文件类型
  12.  private $errorNum = 0;//错误号
  13.  private $errorMessg = array();//错误消息
  14.  //对成员初始化
  15.  function __construct($options = array()){
  16.  foreach($options as $key=>$val){
  17.   $key = strtolower($key);
  18.   //查看传进来的数组里下标是否与成员属性相同
  19.   //print_r(array_keys(get_class_vars(get_class($this))));
  20.   if(!in_array($key,array_keys(get_class_vars(get_class($this))))){
  21.   continue;
  22.   }else{
  23.   $this->setOption($key,$val);
  24.   }
  25.  }
  26.  }
  27.  private function setOption($key,$val){
  28.    $this->$key = $val;
  29.  //echo $this->errorNum."<br>";
  30.  }
  31.  //检查文件上传路径
  32.  private function checkfilePath(){
  33.  //echo $this->filepath;
  34.  if(emptyempty($this->filepath)){
  35.   $this->setOption('errorNum',"-5");
  36.   return false;
  37.  }
  38.  if(!file_exists($this->filepath) || !is_writable($this->filepath)){
  39.   if(!@mkdir($this->filepath,0755)){
  40.   $this->setOption('errorNum','-4');
  41.   return false;
  42.   }
  43.  }
  44.  return true;
  45.  }
  46.  //获取错误信息
  47.  private function getError(){
  48.  $str = "上传文件{$this->originName}出错---";
  49.  switch($this->errorNum){
  50.   case 4; $str .= "没有文件被上传";break;
  51.   case 3; $str .= "文件只被部分上传";break;
  52.   case 2; $str .= "超过文件表单允许大小";break;
  53.   case 1; $str .= "超过php.ini中允许大小";break;
  54.   case -1; $str .= "未允许的类型";break;
  55.   case -2; $str .= "文件过大,不能超过".$this->maxsize."个字节";break;
  56.   case -3; $str .= "上传失败";break;
  57.   case -4; $str .= "建立文件上传目录失败";break;
  58.   case -5; $str .= "必须指定上传路径";break;
  59.   default$str .= "未知错误";
  60.  }
  61.  return $str."<br>";
  62.  }
  63.  //检查文件类型
  64.  private function checkfileType(){
  65.  //echo $this->fileType;
  66.  if(!in_array(strtolower($this->fileType),$this->allowtype)){
  67.  $this->setOption('errorNum','-1');
  68.   return false;
  69.  }else{
  70.   return true;
  71.  }
  72.  }
  73.  //检查文件大小
  74.  private function checkfileSize(){
  75.  if($this->fileSize > $this->maxsize){
  76.   $this->setOption('errorNum','-2');
  77.   return false;
  78.  }else{
  79.   return true;
  80.  }
  81.  }
  82.  //处理随机文件名称
  83.  private function prorandFile(){
  84.  $ch = $this->israndfile;
  85.  if($ch == 'true'){
  86.   return true;
  87.  }else{
  88.   return false;
  89.  }
  90.  }
  91.  //
  92.  private function setFiles($name="",$tmp_name="",$size="",$error=""){
  93.  //检查上传路径
  94.  if(!$this->checkfilePath()){
  95.   //$this->errorMessg = $this->getError();
  96.   return false;
  97.  }
  98.  //echo $error."<br>";
  99.  if($error){
  100.  $this->setOption('errorNum',$error);
  101.   return false;
  102.  }
  103.  $arrstr  = explode('.',$name);
  104.  $type   = end($arrstr);
  105.  $this->setOption('originName',$name);
  106.  $this->setOption('fileSize',$size);
  107.  $this->setOption('fileType',$type);
  108.  $this->setOption('tmpfileName',$tmp_name);
  109.  return true;
  110.  }
  111.  //检查是否有文件上传
  112.  function checkFile($formname){
  113.  if(!@$_FILES[$formname]){
  114.   $this->setOption('errorNum',4);
  115.   return false;
  116.  }else{
  117.   return true;
  118.  }
  119.  }
  120.  //上传文件
  121.  function uploadeFile($formname){
  122.  if(!$this->checkFile($formname)){
  123.   $this->errorMessg = $this->getError();
  124.   return false;
  125.  }
  126.  $return  = true;
  127.  $name   = @$_FILES[$formname]['name'];
  128.  $tmp_name = @$_FILES[$formname]['tmp_name'];
  129.  $size   = @$_FILES[$formname]['size'];
  130.  $error  = @$_FILES[$formname]['error'];
  131.  //$type   = $_FILES[$formname]['type'];
  132.  if(is_array($name)){
  133.   $errors = array();
  134.   for($i=0; $i<count($name); $i++){="" if($this-="">setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
  135.    if(!$this->checkfileSize() || !$this->checkfileType()){
  136.    $errors[] = $this->getError();
  137.    $return = false;
  138.    }
  139.   }else{
  140.    $errors[] = $this->getError();
  141.    $return = false;
  142.   }
  143.   if(!$return$this->setFiles();
  144.   }
  145.   if($return){
  146.   $newfileN = array();
  147.   for($i=0; $i<count($name); $i++){="" if($this-="">setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
  148.    if(!$this->copyFile()){
  149.     $errors[] = $this->getError();
  150.     $return = false;
  151.    }else{
  152.     $newfileN[] = $this->newfileName;
  153.    }
  154.    }
  155.    $this->newfileName = $newfileN;
  156.   }
  157.   }
  158.   //print_r($errors);
  159.   $this->errorMessg = $errors;
  160.   //echo $errors;
  161.   return $return;
  162.  }else{
  163.   if($this->setFiles($name,$tmp_name,$size,$error)){
  164.   $return = true;
  165.   if($error) var_dump($error);
  166.   if($this->checkfileSize() && $this->checkfileType()){
  167.   }else{
  168.    $return = false;
  169.   }
  170.   }else{
  171.   $return = false;
  172.   }
  173.   if(!$return){
  174.   $this->errorMessg = $this->getError();
  175.   }
  176.   return $return;
  177.  }
  178.  }
  179.  //获取上传后的文件名
  180.  function getnewFile(){
  181.   return $this->newfileName;
  182.  }
  183.  //把文件拷贝到指定的路径
  184.  function copyFile(){
  185.  $filepath = rtrim($this->filepath,'/')."/";
  186.  if(!$this->errorNum){
  187.   if($this->prorandFile()){
  188.    $this->newfileName = date('Ymdhis').rand(1000,9999).".".$this->fileType;
  189.   }else{
  190.    $this->newfileName = $this->originName;
  191.   }
  192.   if(!move_uploaded_file($this->tmpfileName,$filepath.$this->newfileName)){
  193.   $this->setOption('errorNum',-3);
  194.   return false;
  195.   }else{
  196.   return true;
  197.   }
  198.  }else{
  199.   return false;
  200.  }
  201.  }
  202.  //上传错误后返回的消息
  203.  function gteerror(){
  204.   $err = $this->errorMessg;
  205.  return $err;
  206.  }
  207.  }
  208. ?>
  209. </count($name);></count($name);>

2、使用方法

uploade.php 文件:

  1. <?php
  2. //print_r($_FILES['spic']);
  3. header('Content-Type:text/html;charset=utf-8');
  4. //if(@$_FILES['spic'])echo "ddddddddd";;
  5. include('upFiles.css.php');
  6. $upfile = new UploadFiles(array('filepath'=>'./upload','allowtype'=>array('php','bmp','gif','jpg','png'),'israndfile'=>true,'maxsize'=>'1000000'));
  7. if($upfile ->uploadeFile('spic')){
  8.  $arrfile = $upfile ->getnewFile();
  9.  foreach($arrfile as $v){
  10.  echo $v,"<br>";
  11.  }
  12.  echo "上传成功!";
  13. }else{
  14.  $err = $upfile ->gteerror();
  15.  if(is_array($err)){
  16.  foreach($err as $v1){
  17.   echo $v1,"<br>";
  18.  }
  19.  }else{
  20.  echo $err;
  21. //
  22.  }
  23.  //var_dump($err);
  24. }
  25. //var_dump($upfile);
  26. ?>

HTML 文件:

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  2. <title>无标题文档</title>
  3. <script type="text/javascript">
  4. function Check(){
  5.  //alert('dddd');
  6.  for(i=1; i<9; i++){
  7.  if(document.getElementById('v'+i).value == ''){
  8.   document.getElementById('v'+i).name = 'uu';
  9.  }
  10.  }
  11. }
  12. </script>
  13. <form name="upfile" action="uploade.php" method="post" enctype="multipart/form-data">
  14. <input type="file" name="spic[]" id="v1"><br>
  15. <input type="file" name="spic[]" id="v2"><br>
  16. <input type="file" name="spic[]" id="v3"><br>
  17. <input type="file" name="spic[]" id="v4"><br>
  18. <input type="file" name="spic[]" id="v5"><br>
  19. <input type="file" name="spic[]" id="v6"><br>
  20. <input type="file" name="spic[]" id="v7"><br>
  21. <input type="file" name="spic[]" id="v8"><br>
  22. <input type="submit" name="sub" value="提交" onclick="return Check()">
  23. <input type="reset" name="res" value="重填">
  24. </form>

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录