php 高级完美多文件上传类程序

<?php
$action = $_GET['action'];
require_once('auc.main.class.inc.php');

$auc = new auc();

if ($action == 'uploadfile') {
 $auc = new auc();
 
 $result = $auc->upload("file");
 if (is_array($result)) {
  echo 'Something Went Wrong';
  echo '<pre>';
  var_dump($result);
  echo '</pre>';
 } else {
  echo 'All OK';
 }
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>advanced Upload Class - Demo</title>
</head>
<body>
<form action="auc.demo.php?action=uploadfile" method="post" enctype="multipart/form-data">
  <input name="file[]" type="file" /><br />
  <input name="file[]" type="file" /><br />
  <input name="upload" type="submit" value="Upload File" />
</form>
</body>
</html>
<?php  } ?>

类文件

<?php

class auc {
 public $errors = array(); //array used to store any errors that occur.
 public $upload_dir = ''; //the upload_dir being used by the script
 public $make_safe = false; //default don't modify the file name to safe version
 public $max_file_size = 1048576; //Max File Size in Bytes, 1MB
 public $overwrite = false; //default don't overwrite files that already exsist
 public $check_file_type = false; //don't check for file type by default but can check for allowed and denied files.
 public $allowed_mime_types = array('image/jpeg', 'image/png', 'image/gif', 'image/tiff'); //array of allowed mime types used when check_file_type is set to allowed
 public $denied_mime_types = array('application/x-php', 'text/html'); //array of denied mime types used when check_file_type is set to denied
 
 /**
  * Check if the upload dir is valid, if it is not valid attempt to make the dir, if dir is succesfully created chmod it to 0777.
  * If any elments fail return false else set upload_dir and return true.
  * @param string $dir
  * @param boolean $mkdir
  * @return true or false
  */
 public function upload_dir($dir, $mkdir = false) {
  $errors =& $this->errors;
  $status = true;
  
  if (!is_dir($dir)) {
   if ($mkdir) {
    if (!mkdir($dir)) {
     $status = false;
    } else {
     if (!chmod($dir, 0777)) $status = false;
    }
   } else {
    $status = false;
   }
  }
  
  if ($status) {
   $this->upload_dir = $dir;
   return true;
  } else {
   $errors['general'][] = 'Upload Dir is Not Valid and/or a dir could not be created/chmod.';
   return false;
  }
 }
 
 /**
  * check that the upload dir is valid and that it is writeable
  *
  * @param string $dir
  * @return true or false
  */
 public function check_dir($dir) {
  if (!is_dir($dir) || !is_writable($dir)) return false;
  
  return true;
 }
 

 /**
  * make the uploaded file name safe
  *
  * @param string $file_name
  * @return safe file name
  */
 public function make_safe($file_name) {
  return str_replace(' ', '_', $file_name);
 }
  
 /**
  * Check the Attemted Uploads for errors etc if everything goes good move the file, to the upload_dir.
  *
  * @param array $object
  * @return unknown
  */
 public function upload($object) {
  $errors =& $this->errors;
  
  if (empty($errors['general'])) {
   if (empty($this->upload_dir)) $this->upload_dir = dirname(__FILE__).'/'; //if no default upload_dir has been specified used the current dir.
     
   if ($this->check_dir($this->upload_dir)) {
    $files = $_FILES[$object];
    $count = count($files['name']) - 1;
    
    echo '<pre>';
    var_dump($files);
    echo '</pre>';
    
    for ($current = 0; $current <= $count; $current++) {
     $error = '';
     try {
      //check for $_FILES Errors
      switch ($files['error'][$current]) {
       case 0 : break;
       case 1 : $error = $files['name'][$current].' exceeds the upload_max_filesize directive in php.ini'; break;
       case 2 : $error = $files['name'][$current].' exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; break;
       case 3 : $error = $files['name'][$current].' was only partially uploaded'; break;
       case 4 : $error = 'No file was uploaded'; break;
       case 6 : $error = 'Missing a temporary folder'; break;
       case 7 : $error = 'Failed to write '.$files['name'][$current].' to disk'; break;
       case 8 : $error = $files['name'][$current].' stopped by extension'; break;
       default : $error = 'Unidentified Error, caused by '.$files['name'][$current]; break;
      }
      if ($error)
       throw new TrigerErrorException($error, $files['name'][$current]);
      
      //check that the file is not empty
      if ($files['size'][$current] <= 0)
       throw new TrigerErrorException($files['name'][$current].' is empty', $files['name'][$current]);
      
      //check that the file does not exceed the defined max_file_size
      if ($this->max_file_size) {
       if ($files['size'][$current] >= $this->max_file_size)
        throw new TrigerErrorException($files['name'][$current].' exceeds defined max_file_size', $files['name'][$current]);
      }
      
      if ($this->check_file_type == 'allowed' && !in_array($files['type'][$current], $this->allowed_mime_types)) {
       throw new TrigerErrorException($files['name'][$current].' is not an allowed type', $files['name'][$current]);
      } elseif ($this->check_file_type == 'denied' && in_array($files['type'][$current], $this->denied_mime_types)) {
       throw new TrigerErrorException($files['name'][$current].' is a denied type', $files['name'][$current]);
      }
      
      //if make_safe is true call make safe function  
      if ($this->make_safe)
       $files['name'][$current] = $this->make_safe($files['name'][$current]);
      
      //if overwrite is false and the file exists error
      if (!$this->overwrite && file_exists($this->upload_dir.$files['name'][$current]))
       throw new TrigerErrorException($files['name'][$current].' already exsists', $files['name'][$current]);
       
      //move the uploaded file, error if anything goes wrong.
      if (!move_uploaded_file($files['tmp_name'][$current], $this->upload_dir.$files['name'][$current]))
       throw new TrigerErrorException($files['name'][$current].' could not be moved', $files['name'][$current]);
     } catch (TrigerErrorException $e) {
      $errors[$files['name'][$current]][] = $e->Message();
     }
    }
    
    if (empty($errors)) {
     //return true if there where no errors
     return true;
    } else {
     //return the errors array if there where any errros
     return $errors;
    }
   } else {
    //return false as dir is not valid
    $errors['general'][] = "The Specified Dir is Not Valid or is Not Writeable";
    return false;
   }
  }
 } 
}

/**
 * Handle the Exceptions trigered by errors within upload code.
 *
 */
class TrigerErrorException extends Exception {
 protected $file = "";
 public function __construct($message, $file = "", $code = 0) {
  $this->file = $file;
     parent::__construct($message, $code);
 }

   public function Message() {
  return "{$this->message}";
    }
}
?>

时间: 2024-11-14 11:57:41

php 高级完美多文件上传类程序的相关文章

php文件上传类程序代码

 代码如下 复制代码 <?php  /**   * 文件上传类   */  class uploadFile {       public $max_size = '1000000';//设置上传文件大小     public $file_name = 'date';//重命名方式代表以时间命名,其他则使用给予的名称     public $allow_types;//允许上传的文件扩展名,不同文件类型用"|"隔开     public $errmsg = '';//错误信息  

一个完美php文件上传类使用实例

今天终于算是可以来更新一下paperen的网站,嗯--因为写毕设论文写累了所以就发表一篇关于上传文件类的东东吧,这个类也是自己写的,不知道对大家有没有用,如果觉得可以就拿去用吧.不过在编码上面还是没做好,自己是在utf8的页面将数据进行提交的,所以生成的文件如果保持原来的中文名字的话在文件夹中看到就是乱码的名字,如果不想出现乱码的话需要进行一下编码的转换.?  代码如下 复制代码 <?php define('NO_FILE', '不存在上传文件'); define('NOT_ALLOW_EXT'

PHP中codeigniter文件上传类代码实例

  codeigniter文件上传类代码实例 文件上传类 CodeIgniter 的文件上传类允许文件被上传.您可以设置指定上传某类型的文件及指定大小的文件. 处理过程 上传文件普遍的过程: 一个上传文件用的表单,允许用户选择一个文件并上传它. 当这个表单被提交,该文件被上传到指定的目录. 同时,该文件将被验证是否符合您设定的要求. 一旦文件上传成功,还要返回一个上传成功的确认窗口. 这里有一个简短的教程来显示这个过程.此后你将会找到相关的参考信息. 创建上传表单 运用文本编辑器创建一个名为up

经典的PHP文件上传类

在课堂上给大家写了一个经典的文件上传类,灵活性还可以,大家可以参考使用,   上传文件:   <?php /** * author : PHP100.com * date :2012-9-15 经典的文件上传类  **/  if(!empty($_POST['sub'])){   include("up.class.php");      $up = new up($_FILES['up']); //参数,文件流必选,[指定目录,指定大小,指定文件名]可选  }  ?>  

ASP.NET实现的简单易用文件上传类

  这篇文章主要介绍了ASP.NET实现的简单易用文件上传类,本文给出实现代码和使用方法示例,需要的朋友可以参考下 调用方法: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 UploadFile uf = new UploadFile();   /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认:true),true原始文件名,false系统生成新文件名

PHP多文件上传类

  PHP多文件上传类 /* 多文件上传类 修改:Linvo 2008-2-15 */ class more_file_upload{ const FILE_PATH='../upfileclass/uploadfile/'; var file_type; var file_type_array; var file_type_real_array; var file_type_string; var file_name; var file_size; var file_tmp_name; var

功能强大的php文件上传类_php技巧

本文实例为大家分享了php文件上传类,功能很强大,供大家参考,具体内容如下 <?PHP /* *文件上传类 **/ class upfile{ private $file_size;//上传源文件大小 private $file_tem;//上传文件临时储存名 private $file_name;//上传文件名 private $file_type;//上传文件类型 private $file_max_size=2000000;//允许文件上传最大 private $file_folder=&qu

一个经典的PHP文件上传类分享_php实例

文件上传是项目开发中比较常见的功能,但文件上传的过程比较繁琐,只要是有文件上传的地方就需要编写这些复杂的代码.为了能在每次开发中降低功能的编写难度,也为了能节省开发时间,通常我们都会将这些反复使用的一段代码封装到一个类中.帮助开发者在以后的开发中,通过编写几条简单代码就可以实现复杂的文件上传功能.对于基础薄弱的读者,只要会使用本类即可,而对一些喜欢挑战的朋友,可以尝试去读懂它,并能开发一个属于自己的文件上传类. 一.需求分析 要球自定义文件上传类,即在使用非常简便的前提下,又可以完成以下几项功能

PHP多文件上传类实例_php技巧

本文实例讲述了PHP多文件上传类.分享给大家供大家参考.具体如下: 复制代码 代码如下: <?php /* PHP多文件上传类 修改:Linvo 2008-2-15 */ class more_file_upload{     const FILE_PATH='../upfileclass/uploadfile/';     var $file_type;     var $file_type_array;     var $file_type_real_array;     var $file