自己写的php上传类,完全独立的 亲测好用

来源:互联网 发布:两小无猜网络剧小说 编辑:程序博客网 时间:2024/05/22 02:27

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>page_title</title>
  </head>
<body>
<?php
if($_FILES)
{
 $je_upload=new jetee_upload();
 print_r($_FILES);
 if(is_array($_FILES['file_path']['name']))
 {
  // 上传多文件
  foreach ($_FILES['file_path']['error'] AS $key => $value)
  {
   if ($value == 0)
   {
    if (!$je_upload->check_upload_type($_FILES['file_path']['type'][$key]))
    {
     echo 'invalid_img_url;';//sys_msg(sprintf($_LANG['invalid_img_url'], $key + 1), 1, array(), false);
    }
   }
   elseif ($value == 1)
   {
    echo 'img_url_too_big;';//sys_msg(sprintf($_LANG['img_url_too_big'], $key + 1, $php_maxsize), 1, array(), false);
   }
   elseif ($value == 2)
   {
    echo 'img_url_too_big;';//sys_msg(sprintf($_LANG['img_url_too_big'], $key + 1, $htm_maxsize), 1, array(), false);
   }
  }
 }
 else//单文件
 {
  if ($_FILES['file_path']['error'] == 0)
  {
   if (!$je_upload->check_upload_type($_FILES['file_path']['type']))
   {
    echo 'invalid_img_url;';//sys_msg(sprintf($_LANG['invalid_img_url'], $key + 1), 1, array(), false);
   }
  }
  elseif ($_FILES['file_path']['error'] == 1)
  {
   echo 'img_url_too_big;';//sys_msg(sprintf($_LANG['img_url_too_big'], $key + 1, $php_maxsize), 1, array(), false);
  }
  elseif ($_FILES['file_path']['error'] == 2)
  {
   echo 'img_url_too_big;';//sys_msg(sprintf($_LANG['img_url_too_big'], $key + 1, $htm_maxsize), 1, array(), false);
  }
 }
 print_r( $je_upload->upload_file($_FILES['file_path'], 'E:/w/work/jetee.cn/data/captcha/'));
}

Class jetee_upload{
 /**
  * 检查上传文件类型是否合法  允许的png|jpg|jpeg|gif|doc|xls|txt|zip|ppt|pdf|rar
  * @access  public
  * @param string  $file_type   $_FILES['file_path']['type']
  * @return bool
  */   
 public function check_upload_type($file_type) {
  return $file_type == 'image/pjpeg' ||
      $file_type == 'image/x-png' ||
      $file_type == 'image/png'   ||
      $file_type == 'image/gif'   ||
      $file_type == 'image/jpeg'  ||
      $file_type == 'application/msword'   ||
      $file_type == 'application/vnd.ms-excel'   ||
      $file_type == 'text/plain'   ||
      $file_type == 'application/zip'   ||
      $file_type == 'application/vnd.ms-powerpoint'   ||
      $file_type == 'application/pdf' ||
      $file_type == 'application/octet-stream' ||
      $file_type == 'application/x-rar-compressed';
     
 }
 /**
  * 处理上传文件,并返回上传图片名(上传失败时返回图片名为空)
  *
  * @access  public
  * @param array     $upload     $_FILES 数组
  * @param array     $type       上传的绝对路径 e:/w/work/jetee.cn/data/captcha/
  *
  * @return string or arrary     上传图片名
  */
 public function upload_file($upload, $type)
 {
  $multi=is_array($upload[name]);
  if($multi)//多个文件上传
  {
   foreach($upload['tmp_name'] as $k=>$v){
    if (!empty($v)){
     $ftype = $this->check_file_type($v, $upload['name'][$k], '|png|jpg|jpeg|gif|doc|xls|txt|zip|ppt|pdf|rar|');
     if (!empty($ftype)){
      $name = date('YmdHis');
      for ($i = 0; $i < 6; $i++){
       $name .= chr(mt_rand(97, 122));
      }
      $name = $name . '.' . $ftype;
      $target = $type . $name;
      if (!$this->move_upload_file($v, $target)){       
       return false;
      }
      else{
       $multi_name[]=$name;
       if(count($upload[name])-1==$k) return $multi_name;
      }
     }
     else{ 
      return false;
     }
    }
    else
    {      
     return false;
    }   
   }
  }
  else//单个文件上传
  {
   if (!empty($upload['tmp_name'])){
    $ftype = $this->check_file_type($upload['tmp_name'], $upload['name'], '|png|jpg|jpeg|gif|doc|xls|txt|zip|ppt|pdf|rar|');
    if (!empty($ftype)){
     $name = date('YmdHis');
     for ($i = 0; $i < 6; $i++){
      $name .= chr(mt_rand(97, 122));
     }
     $name = $name . '.' . $ftype;
     $target = $type . $name;
     if (!$this->move_upload_file($upload['tmp_name'], $target)){
      
      return false;
     }
     else{
      return $name;
     }
    }
    else{
     return false;
    }
   }
   else{    
    return false;
   }
  }
 }

 
 /**
  * 检查文件类型
  *
  * @access      public
  * @param       string      filename            文件名
  * @param       string      realname            真实文件名
  * @param       string      limit_ext_types     允许的文件类型
  * @return      string
  */
 private function check_file_type($filename, $realname = '', $limit_ext_types = '')
 {
  if ($realname){
   $extname = strtolower(substr($realname, strrpos($realname, '.') + 1));
  }
  else{
   $extname = strtolower(substr($filename, strrpos($filename, '.') + 1));
  }

  if ($limit_ext_types && stristr($limit_ext_types, '|' . $extname . '|') === false){
   return '';
  }

  $str = $format = '';

  $file = @fopen($filename, 'rb');
  if ($file)
  {
   $str = @fread($file, 0x400); // 读取前 1024 个字节
   @fclose($file);
  }
  else//如果无内容。
  {
   if (stristr($filename, $_SERVER['DOCUMENT_ROOT']) === false)
   {
    if ($extname == 'jpg' || $extname == 'jpeg' || $extname == 'gif' || $extname == 'png' || $extname == 'doc' ||
     $extname == 'xls' || $extname == 'txt'  || $extname == 'zip' || $extname == 'rar' || $extname == 'ppt' ||
     $extname == 'pdf' || $extname == 'rm'   || $extname == 'mid' || $extname == 'wav' || $extname == 'bmp' ||
     $extname == 'swf' || $extname == 'chm'  || $extname == 'sql' || $extname == 'cert')
    {
     $format = $extname;
    }
   }
   else
   {
    return '';
   }
  }

  if ($format == '' && strlen($str) >= 2 )
  {
   if (substr($str, 0, 4) == 'MThd' && $extname != 'txt')
   {
    $format = 'mid';
   }
   elseif (substr($str, 0, 4) == 'RIFF' && $extname == 'wav')
   {
    $format = 'wav';
   }
   elseif (substr($str ,0, 3) == "\xFF\xD8\xFF")
   {
    $format = 'jpg';
   }
   elseif (substr($str ,0, 4) == 'GIF8' && $extname != 'txt')
   {
    $format = 'gif';
   }
   elseif (substr($str ,0, 8) == "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
   {
    $format = 'png';
   }
   elseif (substr($str ,0, 2) == 'BM' && $extname != 'txt')
   {
    $format = 'bmp';
   }
   elseif ((substr($str ,0, 3) == 'CWS' || substr($str ,0, 3) == 'FWS') && $extname != 'txt')
   {
    $format = 'swf';
   }
   elseif (substr($str ,0, 4) == "\xD0\xCF\x11\xE0")
   {   // D0CF11E == DOCFILE == Microsoft Office Document
    if (substr($str,0x200,4) == "\xEC\xA5\xC1\x00" || $extname == 'doc')
    {
     $format = 'doc';
    }
    elseif (substr($str,0x200,2) == "\x09\x08" || $extname == 'xls')
    {
     $format = 'xls';
    } elseif (substr($str,0x200,4) == "\xFD\xFF\xFF\xFF" || $extname == 'ppt')
    {
     $format = 'ppt';
    }
   } elseif (substr($str ,0, 4) == "PK\x03\x04")
   {
    $format = 'zip';
   } elseif (substr($str ,0, 4) == 'Rar!' && $extname != 'txt')
   {
    $format = 'rar';
   } elseif (substr($str ,0, 4) == "\x25PDF")
   {
    $format = 'pdf';
   } elseif (substr($str ,0, 3) == "\x30\x82\x0A")
   {
    $format = 'cert';
   } elseif (substr($str ,0, 4) == 'ITSF' && $extname != 'txt')
   {
    $format = 'chm';
   } elseif (substr($str ,0, 4) == "\x2ERMF")
   {
    $format = 'rm';
   } elseif ($extname == 'sql')
   {
    $format = 'sql';
   } elseif ($extname == 'txt')
   {
    $format = 'txt';
   }
  }

  if ($limit_ext_types && stristr($limit_ext_types, '|' . $format . '|') === false)
  {
   $format = '';
  }

  return $format;
 } 
 
 
 /**
  * 将上传文件转移到指定位置
  *
  * @param string $file_name
  * @param string $target_name
  * @return blog
  */
 private function move_upload_file($file_name, $target_name = '')
 {
  if (function_exists("move_uploaded_file"))
  {
   if (move_uploaded_file($file_name, $target_name))
   {
    @chmod($target_name,0755);
    return true;
   }
   else if (copy($file_name, $target_name))
   {
    @chmod($target_name,0755);
    return true;
   }
  }
  elseif (copy($file_name, $target_name))
  {
   @chmod($target_name,0755);
   return true;
  }
  return false;
 } 
 
 
 
}
?>
<form enctype="multipart/form-data" action="#" method="post" name="theForm" >
    <!-- 最大文件限制 -->
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
    <!--input type="file" name="file_path" /-->  
    <input type="file" name="file_path[]" />  
    <input type="file" name="file_path[]" />  
    <input type="submit" value="上传文件">
</form>

</body>
</html>

原创粉丝点击