首页 php教程 php手册 仿开源中国,分享代码时候的多文件上传

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

Jun 06, 2016 pm 07:32 PM
上传 中国 代码 分享 开源 文件 标题党

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

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

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

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

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

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

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

仿开源中国,分享代码时候的多文件上传 仿开源中国,分享代码时候的多文件上传
<?php
//http://www.codepearl.com
$action = isset($_GET['action'])?$_GET['action']:"";
require_once('main.class.php');

$auc = new auc();

if ($action == 'uploadfile') {
	$auc = new auc();
	//Comment: $auc->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 '<pre class="brush:php;toolbar:false">';
		var_dump($result);
		echo '
登录后复制
'; } else { echo 'All OK'; } } else { ?> Upload Class - Demo


<?php
//http://www.codepearl.com
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 class="brush:php;toolbar:false">';
				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}"; } } ?>
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

十个推荐开源免费文本标注工具 十个推荐开源免费文本标注工具 Mar 26, 2024 pm 08:20 PM

文本标注工作是将标签或标记与文本中特定内容相对应的工作。其主要目的是为文本提供额外的信息,以便进行更深入的分析和处理,尤其是在人工智能领域。文本标注对于人工智能应用中的监督机器学习任务至关重要。用于训练AI模型,有助更准确地理解自然语言文本信息,提高文本分类、情感分析和语言翻译等任务的性能。通过文本标注,我们可以教AI模型识别文本中的实体、理解上下文,并在出现新的类似数据时做出准确的预测。本文主要推荐一些较好的开源文本标注工具。1.LabelStudiohttps://github.com/Hu

15个值得推荐的开源免费图像标注工具 15个值得推荐的开源免费图像标注工具 Mar 28, 2024 pm 01:21 PM

图像标注是将标签或描述性信息与图像相关联的过程,以赋予图像内容更深层次的含义和解释。这一过程对于机器学习至关重要,它有助于训练视觉模型以更准确地识别图像中的各个元素。通过为图像添加标注,使得计算机能够理解图像背后的语义和上下文,从而提高对图像内容的理解和分析能力。图像标注的应用范围广泛,涵盖了许多领域,如计算机视觉、自然语言处理和图视觉模型具有广泛的应用领域,例如,辅助车辆识别道路上的障碍物,帮助疾病的检测和诊断通过医学图像识别。本文主要推荐一些较好的开源免费的图像标注工具。1.Makesens

出现0x80004005错误代码怎么办 小编教你0x80004005错误代码解决方法 出现0x80004005错误代码怎么办 小编教你0x80004005错误代码解决方法 Mar 21, 2024 pm 09:17 PM

在电脑中删除或解压缩文件夹,时有时候会弹出提示对话框“错误0x80004005:未指定错误”,如果遇到这中情况应该怎么解决呢?提示错误代码0x80004005的原因其实有很多,但大部分因为病毒导致,我们可以重新注册dll来解决问题,下面,小编给大伙讲解0x80004005错误代码处理经验。有用户在使用电脑时出现错误代码0X80004005的提示,0x80004005错误主要是由于计算机没有正确注册某些动态链接库文件,或者计算机与Internet之间存在不允许的HTTPS连接防火墙所引起。那么如何

网易云音乐怎么分享到微信朋友圈_网易云音乐分享到微信朋友圈教程 网易云音乐怎么分享到微信朋友圈_网易云音乐分享到微信朋友圈教程 Mar 25, 2024 am 11:41 AM

1、首先我们进入到网易云音乐中,然后在软件首页界面中,点击进入到歌曲的播放界面中。2、然后在歌曲播放界面中,找到右上方的分享功能按钮,如下图红框所示位置,点击选择分享的渠道;在分享渠道中,点击底部的“分享至”选项,然后选择第一个“微信朋友圈”,即可将内容分享至微信朋友圈。

推荐:优秀JS开源人脸检测识别项目 推荐:优秀JS开源人脸检测识别项目 Apr 03, 2024 am 11:55 AM

人脸检测识别技术已经是一个比较成熟且应用广泛的技术。而目前最为广泛的互联网应用语言非JS莫属,在Web前端实现人脸检测识别相比后端的人脸识别有优势也有弱势。优势包括减少网络交互、实时识别,大大缩短了用户等待时间,提高了用户体验;弱势是:受到模型大小限制,其中准确率也有限。如何在web端使用js实现人脸检测呢?为了实现Web端人脸识别,需要熟悉相关的编程语言和技术,如JavaScript、HTML、CSS、WebRTC等。同时还需要掌握相关的计算机视觉和人工智能技术。值得注意的是,由于Web端的计

酷狗上传自己的音乐的简单步骤 酷狗上传自己的音乐的简单步骤 Mar 25, 2024 pm 10:56 PM

1、打开酷狗音乐,点击个人头像。2、点击右上角设置的图标。3、点击【上传音乐作品】。4、点击【上传作品】。5、选择歌曲,然后点击【下一步】。6、最后点击【上传】即可。

阿里7B多模态文档理解大模型拿下新SOTA 阿里7B多模态文档理解大模型拿下新SOTA Apr 02, 2024 am 11:31 AM

多模态文档理解能力新SOTA!阿里mPLUG团队发布最新开源工作mPLUG-DocOwl1.5,针对高分辨率图片文字识别、通用文档结构理解、指令遵循、外部知识引入四大挑战,提出了一系列解决方案。话不多说,先来看效果。复杂结构的图表一键识别转换为Markdown格式:不同样式的图表都可以:更细节的文字识别和定位也能轻松搞定:还能对文档理解给出详细解释:要知道,“文档理解”目前是大语言模型实现落地的一个重要场景,市面上有很多辅助文档阅读的产品,有的主要通过OCR系统进行文字识别,配合LLM进行文字理

百度网盘怎么分享文件给好友 百度网盘怎么分享文件给好友 Mar 25, 2024 pm 06:52 PM

近期,百度网盘安卓客户端迎来了全新的8.0.0版本,这一版本不仅带来了众多变化,还增添了诸多实用功能。其中,最为引人注目的便是文件夹共享功能的增强。现在,用户可以轻松邀请好友加入,共同分享工作和生活中的重要文件,实现更加便捷的协作与共享。那么究竟该如何分享给好友自己需要分享的文件呢,下文中本站小编就将为大家带来详细内容介绍,希望能帮助到大家!1)打开百度云APP,首先点击在首页中选择相关的文件夹,然后再点击界面右上角的【...】图标;(如下图)2)随后点击“共享成员”一栏中的【+】,最后在勾选所

See all articles