0

0

仿开源中国,分享代码时候的多文件上传

php中文网

php中文网

发布时间:2016-06-06 19:32:31

|

1127人浏览过

|

来源于php中文网

原创

有点标题党,不过确实是一个不错的多文件上传类。简洁实用。 这个类可以用来处理表单上传多个文件。 这个类可以检查文件是否不为空,也没有超过给定的大小限制。 它也可以检查文件类型对允许和拒绝的文件类型列表。 该类还可以通过用下划线代替空格使最终的文

有点标题党,不过确实是一个不错的多文件上传类。简洁实用。
这个类可以用来处理表单上传多个文件。

这个类可以检查文件是否不为空,也没有超过给定的大小限制。

它也可以检查文件类型对允许和拒绝的文件类型列表。

该类还可以通过用下划线代替空格使最终的文件名更安全。

如果在上传文件时出现错误,类抛出一个异常对象,该对象提供有关错误代码和说明错误消息的信息。
代码珠玑:http://www.codepearl.com/files/194.html

源码与演示:源码出处 演示出处

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

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

下载
仿开源中国,分享代码时候的多文件上传仿开源中国,分享代码时候的多文件上传
upload_dir("directory name", "create dir if it does not exist, false by default or true");
	//$auc->upload_dir("/path/to/uploads/folder/with/trailing/slash/", false);
	//Comment: $auc->make_safe = true || false (default); make the file name safe
	//$auc->make_safe = true;
	//Comment: $auc->max_file_size = size in bytes (1MB default) || false; set max file size in bytes or false not to check size
	//$auc->max_file_size = 1048576;
	//Comment: $auc->overwrite = true || false (default); overwrite if file exists
	//$auc->overwrite = true;
	//Comment: $auc->check_file_type = false (default) || allowed || denied; 
	//$auc->check_file_type = 'allowed';	
	$result = $auc->upload("file");
	if (is_array($result)) {
		echo 'Something Went Wrong';
		echo '
';
		var_dump($result);
		echo '
'; } else { echo 'All OK'; } } else { ?> Upload Class - Demo


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

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

相关专题

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

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

3

2026.01.09

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

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

7

2026.01.09

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

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

11

2026.01.09

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

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

1

2026.01.09

python学习网站
python学习网站

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

4

2026.01.09

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

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

9

2026.01.09

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

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

14

2026.01.09

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

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

2

2026.01.09

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

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

6

2026.01.09

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Node.js 教程
Node.js 教程

共57课时 | 8.3万人学习

Rust 教程
Rust 教程

共28课时 | 4.3万人学习

Vue 教程
Vue 教程

共42课时 | 6.2万人学习

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

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