仿开源中国,分享代码时候的多文件上传
来源:互联网 发布:js给value赋值 编辑:程序博客网 时间:2024/06/07 21:54
<?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>'; 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=utf-8" /><title>Upload Class - Demo</title></head><body><form action="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//http://www.codepearl.comclass 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}"; }}?>
0 0
- 仿开源中国,分享代码时候的多文件上传
- Hadoop上传文件时候的错误
- 文件上传时候可能遇到的问题
- 分享非常有用的Java程序 (关键代码) (二)---JavaSript获取上传文件路径的文件名
- 多文件上传的后台代码实现
- 在网上找的一个Flex文件上传代码,记录下来,以后用到的时候可以马上能用:
- 中国开源社区的android分享的代码
- Python的文件上传代码
- 简单的文件上传代码
- 上传文件图片的代码
- 上传文件代码的实现
- 上传单个文件的代码
- 文件上传的基本代码
- 客户端文件上传分享
- 多文件上传类代码
- html5多文件上传代码
- fck在上传文件的时候 FCKeditor 报错
- django批量上传文件的时候报错MultiValueDictKeyError
- 状态机,层次状态机和行为树的区别以及联系
- CS领域会议最佳论文集合 Best Paper Awards in Computer Science (since 1996)
- 大一在ACM的一些感想
- FileUtils 文件工具类org.apache.commons.io.FileUtils
- Android关机流程源码分析
- 仿开源中国,分享代码时候的多文件上传
- MySQL高可用系列之MHA(一)
- sql学习笔记
- C语言操作Excel表格
- 多字节字符和宽字符
- 01背包(二维数组)+方案记录
- JVM
- rabbitmq学习笔记
- android——获取屏幕分辨率——Activity中/非Activity中