0

0

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

php中文网

php中文网

发布时间:2016-06-08 17:28:07

|

1187人浏览过

|

来源于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 '

';
var_dump($result);
echo '
';
 } else {
  echo 'All OK';
 }
} else {
?>




advanced Upload Class - Demo



 

 

 



类文件

名品购物网店系统
名品购物网店系统

适合品牌专卖店专用,从前台的美工设计就开始强调视觉形象,有助于提升商品的档次,打造网店品牌!后台及程序核心比较简洁,着重在线购物,去掉了繁琐的代码及垃圾程式,在结构上更适合一些中高档的时尚品牌商品展示. 率先引入语言包机制,可在1小时内制作出任何语言版本,程序所有应用文字皆引自LANG目录下的语言包文件,独特的套图更换功能,三级物品分类,购物车帖心设计,在国内率先将购物车与商品显示页面完美结合,完

下载

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 '

';
var_dump($files);
echo '
';
    
    for ($current = 0; $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]        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}";
    }
}
?>

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

79

2026.01.09

c++框架学习教程汇总
c++框架学习教程汇总

本专题整合了c++框架学习教程汇总,阅读专题下面的文章了解更多详细内容。

46

2026.01.09

学python好用的网站推荐
学python好用的网站推荐

本专题整合了python学习教程汇总,阅读专题下面的文章了解更多详细内容。

121

2026.01.09

学python网站汇总
学python网站汇总

本专题整合了学python网站汇总,阅读专题下面的文章了解更多详细内容。

12

2026.01.09

python学习网站
python学习网站

本专题整合了python学习相关推荐汇总,阅读专题下面的文章了解更多详细内容。

15

2026.01.09

俄罗斯手机浏览器地址汇总
俄罗斯手机浏览器地址汇总

汇总俄罗斯Yandex手机浏览器官方网址入口,涵盖国际版与俄语版,适配移动端访问,一键直达搜索、地图、新闻等核心服务。

71

2026.01.09

漫蛙稳定版地址大全
漫蛙稳定版地址大全

漫蛙稳定版地址大全汇总最新可用入口,包含漫蛙manwa漫画防走失官网链接,确保用户随时畅读海量正版漫画资源,建议收藏备用,避免因域名变动无法访问。

370

2026.01.09

php学习网站大全
php学习网站大全

精选多个优质PHP入门学习网站,涵盖教程、实战与文档,适合零基础到进阶开发者,助你高效掌握PHP编程。

45

2026.01.09

php网站搭建教程大全
php网站搭建教程大全

本合集专为零基础用户打造,涵盖PHP网站搭建全流程,从环境配置到实战开发,免费、易懂、系统化,助你快速入门建站!

12

2026.01.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 11.4万人学习

Pandas 教程
Pandas 教程

共15课时 | 0.9万人学习

C# 教程
C# 教程

共94课时 | 6.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号