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}";
}
}
?>
class deleteFile{
public $dir ='111cnnet/';
function deldir($this->dir)
{
if(is_dir($dir))
{
$rdir = $dir;
if($dirlist = scandir($rdir))
{
array_shift($dirlist);
array_shift($dirlist);
foreach($dirlist as $d){
$rd = $rdir.'/'.$d;
if(isset($d) && is_file($rd)){
unlink($rd);
}else{
$this->deldir($rd);
}
}
rmdir($rdir);
}else{
return false;
}
}
return true;
}
}
//使用方法
$delete = $this->deleteFile();
class Uploadimg{
private $_fileName=""; //文件域名称 如 'userfile'
private $_uploadDir = ''; //上传路径 如 ./upload/
/*上传参数配置*/
private $_config = array(
'type'=>array('image/jpeg','image/jpg',
'image/pjpeg','image/gif'), //上传的类型
'size'=>1, //文件最大容量单位是M
'width'=>1000, //图片的最大宽度
'height'=>800 //图片的最大高度
);
/**
* 构造函数
*
* @param string $fileName
* @param string $uploadDir
* @param array $config
*/
function __construct($fileName,$uploadDir,$config='')
{
$this->_fileName = $fileName;
$this->_uploadDir = $uploadDir;
if($config == "" or empty($config) or !is_array($config)){
$this->_config = $this->_config;
}else{
$this->_config = $config;
}
}
/**
* 检测容量是否超过
*
* @return boolean
*/
function checkSize()
{
if($_FILES[$this->_fileName]['size'] > $this->_config['size']*1024*1024){
return false;
}else{
return true;
}
}
/**
* 获取图片信息
*
* @return boolean
*/
function getInfo()
{
return @getimagesize($_FILES[$this->_fileName]['tmp_name']);
}
/**
* 检测后缀名
*
* @return boolean
*/
function checkExt()
{
$imageInfo = $this->getInfo();
if(in_array($imageInfo['mime'],$this->_config['type'])){
return true;
}else{
return false;
}
}
/**
* 获取后缀名
*
* @return boolean
*/
function getExt()
{
$imageInfo = $this->getInfo();
switch($imageInfo['mime']){
case 'image/jpeg':$filenameExt = '.jpg';break;
case 'image/jpg':$filenameExt = '.jpg';break;
case 'image/pjpeg':$filenameExt = '.jpg';break;
case 'image/gif':$filenameExt = '.gif';break;
default:break;
}
return $filenameExt;
}
/**
* 检测尺寸
*
* @return boolean
*/
function checkWh()
{
$imageInfo = $this->getInfo();
if(($imageInfo[0] > $this->_config['width']) or ($imageInfo[1] > $this->_config['height'])){
return false;
}else{
return true;
}
}
/**
* 上传一张图片
*
* @return string or int
*/
function uploadSingleImage()
{
if($this->checkSize() == false){
return (-3); /*上传容量过大*/
exit();
}
if($this->checkExt() == true){
$filenameExt = $this->getExt();
}else{
return (-2); /*上传格式错误*/
exit();
}
if($this->checkWh() == false){
return (-1); /*上传图片太宽或太高*/
exit();
}
$file_new_name = date('YmdHis').$filenameExt;
$file_new_name_upload = rtrim($_SERVER['DOCUMENT_ROOT'],'/').$this->_uploadDir.$file_new_name;
if(@move_uploaded_file($_FILES[$this->_fileName]['tmp_name'],$file_new_name_upload)){
return $file_new_name;
}else{
return (0); /*上传失败*/
}
}
/**
* 删除图片
*
* @param string $imageName
* @return boolen
*/
function delImage($imageName)
{
$path = rtrim($_SERVER['DOCUMENT_ROOT'],'/').$this->_uploadDir.$imageName;
if(unlink($path) == true){
return true;
}else{
return false;
}
}
}
class Trees{
private $_keyId = 'Id';
private $_keyName = 'Name';
private $_keyFid = 'Fid';
function __construct($keyId='',$keyName='',$keyFid='')
{
if($keyId==""){$this->_keyId = $this->_keyId;}else{$this->_keyId = $keyId;}
if($keyName==""){$this->_keyName = $this->_keyName;}else{$this->_keyName = $keyName;}
if($keyFid==""){$this->_keyFid = $this->_keyFid;}else{$this->_keyFid = $keyFid;}
}
public function treeListAll($fid,$step=0,&$fromArray,&$resultArray)
{
$step++;
foreach ($fromArray as $k=>$v){
if($v[$this->_keyFid] == $fid){
$newArray[] = $v;
}
}
if(isset($newArray)){
foreach ($newArray as $k=>$v){
$this->treeListAll($v[$this->_keyId],$step,$fromArray,$resultArray);
$v['Step'] = $step;
$resultArray[] = $v;
}
}
}
public function getTreeListAll($fid=0,$step=0,&$fromArray,&$resultArray)
{
$step++;
foreach ($fromArray as $k=>$v){
if($v[$this->_keyFid] == $fid){
$newArray[] = $v;
}
}
if(isset($newArray)){
foreach ($newArray as $k=>$v){
$this->getTreeListAll($v[$this->_keyId],$step,$fromArray,$resultArray);
$v['Step'] = $step;
$resultArray[] = $v;
}
}
}
public function getTreeList($id = 0,&$fromArray)
{
$resultArray = array();
$this->getTreeListAll($id,0,$fromArray,$resultArray);
$resultArray = array_reverse($resultArray);
return $resultArray;
}
public function getTreeArray($id = 0,&$fromArray)
{
$result_one_array = $this->getTreeList($id,$fromArray);
foreach ($result_one_array as $k=>$v){
$result_two_array[] = array($v[$this->_keyId]);
}
if(isset($result_two_array)){
for ($i=0;$i<count($result_two_array);$i++){
for ($j=0;$j<count($result_two_array[$i]);$j++){
$result[] = $result_two_array[$i][$j];
}
}
}else{
$result = array();
}
return $result;
}
public function treeListAllTop($fid,$step=0,&$fromArray,&$resultArray)
{
$step++;
foreach ($fromArray as $k=>$v){
if($v[$this->_keyId] == $fid){
$newArray[] = $v;
}
}
if(isset($newArray)){
foreach ($newArray as $k=>$v){
$this->treeListAllTop($v[$this->_keyFid],$step,$fromArray,$resultArray);
$v['Step'] = $step;
$resultArray[] = $v;
}
}
}
public function getTreeListAllTop($fid=0,$step=0,&$fromArray,&$resultArray)
{
$step++;
foreach ($fromArray as $k=>$v){
if($v[$this->_keyId] == $fid){
$newArray[] = $v;
}
}
if(isset($newArray)){
foreach ($newArray as $k=>$v){
$this->getTreeListAllTop($v[$this->_keyFid],$step,$fromArray,$resultArray);
$v['Step'] = $step;
$resultArray[] = $v;
}
}
}
public function getTreeListTop($id = 0,&$fromArray)
{
$resultArray = array();
$this->getTreeListAllTop($id,0,$fromArray,$resultArray);
$resultArray = array_reverse($resultArray);
return $resultArray;
}
public function getTreeArrayTop($id = 0,&$fromArray)
{
$result_one_array = $this->getTreeListTop($id,$fromArray);
foreach ($result_one_array as $k=>$v){
$result_two_array[] = array($v[$this->_keyFid]);
}
if(isset($result_two_array)){
for ($i=0;$i<count($result_two_array);$i++){
for ($j=0;$j<count($result_two_array[$i]);$j++){
$result[] = $result_two_array[$i][$j];
}
}
}else{
$result = array();
}
return $result;
}
public function makeOptionString($sourcArray,$firstHint="顶级分类",$selectId=array('-1'),$type=0)
{
if($type==0){
if($firstHint != ""){
$str = '<option value="0">'.$firstHint.'</option>';
}else{
$str = '';
}
foreach ($sourcArray as $value){
$level="";
for($i=1;$i<$value['Step'];$i++){
$level =$level."----|";
}
$selectStr = "";
if(in_array($value[$this->_keyId],$selectId)){
$selectStr = "selected";
}else{
}
$str.='<option value="'.$value[$this->_keyId].'" '.$selectStr.'>|'.$level.$value[$this->_keyName]."</option>";
$level="";
}
}else{
$flagStep =-1;
$str = '<option value="0">'.$firstHint.'</option>';
foreach ($sourcArray as $value){
$level="";
for($i=1;$i<$value['Step'];$i++){
$level =$level."----|";
}
$selectStr = "";
if($type==$value[$this->_keyId]){
$flagStep = $value['Step'];
}else{
if($flagStep != -1 && $value['Step']>$flagStep){
}else{
if($flagStep != -1 && $value['Step']<=$flagStep){
$flagStep = -1;
}
if($value[$this->_keyId] == $selectId){
$selectStr = "selected";
}
$str.='<option value="'.$value[$this->_keyId].'" '.$selectStr.'>|'.$level.$value[$this->_keyName]."</option>";
}
}
$level="";
}
}
return $str;
}
}
public function funcStr($str,$num1='',$num2='') //字符正则表达试
{
if($num1!='' and $num2!=''){
return (preg_match("/^[a-zA-Z]{".$num1.",".$num2."}$/",$str))?true:false;
}else{
return (preg_match("/^[a-zA-Z]/",$str))?true:false;
}
}
public function funcNum($str,$num1='',$num2='')//数字正则表达试
{
if($num1!='' and $num2!=''){
return (preg_match("/^[0-9]{".$num1.",".$num2."}$/",$str))?true:false;
}else{
return (preg_match("/^[0-9]/",$str))?true:false;
}
}
public function funcCard($str)//
{
return (preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$str))?true:false;
}
public function funcEmail($str)//邮箱正则表达式
{
return (preg_match('/^[_.0-9a-z-A-Z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$str))?true:false;
}
public function funcPhone($str)//电话号码正则表达试
{
return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,8}$/",$str))?true:false;
}
public function funcMtel($str)//手机号码正则表达试
{
return (preg_match("/(?:13d{1}|15[03689])d{8}$/",$str))?true:false;
}
public function funcZip($str)//邮编正则表达试
{
return (preg_match("/^[0-9]d{5}$/",$str))?true:false;
}
public function funcUrl($str)//url正则表达试
{
return (preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/",$str))?true:false;
}
相关文章
- 本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
- 又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
- 本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24
- 本篇文章主要说明的是与php文件上传的相关配置的知识点。PHP文件上传功能配置主要涉及php.ini配置文件中的upload_tmp_dir、upload_max_filesize、post_max_size等选项,下面一一说明。打开php.ini配置文件找到File Upl...2015-10-21
借助FileReader实现将文件编码为Base64后通过AJAX上传
这篇文章主要介绍了借助FileReader实现将文件编码为Base64后通过AJAX上传的方法,包括后端对文件数据解码并保存的PHP代码,需要的朋友可以参考下...2015-12-25- 这篇文章主要为大家详细介绍了js实现上传文件添加和删除文件选择框 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-25
- 这篇文章主要介绍了jQuery+ajax简单实现文件上传的方法,结合实例形式简单分析了jQuery基于ajax的post方法进行文件传输及asp.net后台处理技巧,需要的朋友可以参考下...2016-06-12
- 本文实例讲述了PHP多文件上传类,分享给大家供大家参考。具体如下:<?phpclass Test_Upload{ protected $_uploaded = array(); protected $_destination; protected $_max = 1024000; protected $_messages =...2015-10-30
- 这篇文章主要介绍了js 实现文件上传样式,下面文章举例说明js 是如何实现文件上传样式的,附有代码详细解说,需要的朋友可以参考一下,希望对你有所帮助...2021-10-21
- php 大文件带进度的上传,一直是一个令php程序员很苦恼的问题。查询baidu 、Google ,大体做带进度的上传方式为:flash+php,socket,apc+php等,下面我介绍了apc +php+ajax制作的带进度的上传,并贴出源码,希望对大家有用。 Altern...2015-10-30
- 这篇文章主要为大家详细介绍了C#文件上传的简单实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 本文给大家介绍一个不错的需要登录的php 文件上传管理系统,功能简单有需要了解的同学可参考。 代码如下<?php$admin_pw="admin";//管理密码$uploaddir="upload";//上传目录session_start();if($_GET['action']=="g...2015-10-30
asp.net html控件的File控件实现多文件上传实例分享
asp.net中html控件的File控件实现多文件上传简单实例,开发工具vs2010使用c#语言,感兴趣的朋友可以了解下,必定是多文件上传值得学习,或许本文所提供的知识点对你有所帮助...2021-09-22- 这篇文章主要为大家介绍了TypeScript前端上传文件到MinIO示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...2022-10-12
- 这篇文章主要介绍了JQuery异步提交表单与文件上传功能,结合实例形式分析了jQuery表单提交及文件传输操作的相关实现技巧,需要的朋友可以参考下...2017-01-16
- 复制代码 代码如下:<?php if($_FILES['myfile']['name'] != '') { if($_FILES['myfile']['error'] > 0) { echo "错误状态:" . $_FILES['myfile']['error']; } else { move_uploaded_f...2013-10-04
使用jQuery.form.js/springmvc框架实现文件上传功能
这篇文章主要介绍了使用jQuery.form.jsspringmvc框架实现文件上传功能,非常具有参考借鉴价值,感兴趣的朋友一起学习吧...2016-05-14jquery插件uploadify实现带进度条的文件批量上传
这篇文章主要介绍了jquery插件uploadify实现带进度条的文件批量上传,感兴趣的小伙伴们可以参考一下...2015-12-14- 这篇文章主要介绍了C#简单实现文件上传功能,利用MVC+EF+LigerUI 实现的upload上传功能,感兴趣的小伙伴们可以参考一下...2020-06-25
- 文件上传中有一块非常重要的就是安全验证了,如果验证不合理就很容易给一些人把此利用上传非常的黑客文件了,那么对于新学php新手文件上传验证有多了解呢?如果不懂可以看...2016-11-25