热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php高级完美多文件上传类程序-PHP源码

ec(2);<?php$action$_GET[action];require_once(auc.main.class.inc.php);$aucnewauc();if($actionuploadfile){ $aucnewauc();  $result$auc->upload(&

$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 {
?>
















类文件

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 <= $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}";
}
}
?>


推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
author-avatar
萝莉控的许123321
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有