原创PHP上传类 瑞意上传类
本人原创的PHP文件上传类。有如下主要功能:
1. 单个上传、批量上传
2. 上传文件大小限制
3. 上传文件扩展名限制
4. 上传文件类型限制
5. 自动创建上传目标目录
6. 自动以时间重命名上传文件
7. 上传结果返回
下载地址:
http://www.
<? //类名: RUpload 瑞意上传类 //Create By Rain, Jan 20 2007 //Update By Rain, Oct 10 2008 //Copyright @ class RUpload { var $max_file_size; //当不大于0时,表示上传文件大小只受服务器配置的上传文件大小限制 var $accept_type = ""; //for example: set_accept_type("image|audio|video") var $refuse_type = ""; //for example: set_refuse_type("application|text") var $accept_ext = ""; //for example: set_accept_ext("jpg|gif|png|mp3|wma") var $refuse_ext = ""; //for example: set_refuse_ext("php|asp|aspx|jsp|exe|com)" var $error = 0; //0:无错误 1:上传失败 2:文件已存在(不覆盖模式上传) 3:非法的文件类型 4:非法的文件扩展名 5:文件大小为零或超出限制 6:创建目录失败 //构造函数 参数: 单个文件大小限制, 允许的扩展名, 拒绝的扩展名, 允许的类型, 拒绝的类型 function RUpload($max_file_size = 0, $accept_ext = '', $refuse_ext = '', $accept_type = '', $refuse_type = '') { $this->max_file_size = $max_file_size <= 0 ? (int)ini_get('upload_max_filesize') * 1048576 : $max_file_size; $this->set_accept_ext($accept_ext); $this->set_refuse_ext($refuse_ext); $this->set_accept_type($accept_type); $this->set_refuse_type($refuse_type); } //把表单中存在的文件全部上传保存到$upload_dir,不改文件名 $overwrite=1时,文件名相同,覆盖原文件 function save($upload_dir = '', $overwrite = 1) { $upload_dir = $this->set_upload_dir($upload_dir); $i = 0; foreach ($_FILES as $file) { $is_ok[$i]['src'] = $file['name']; $is_ok[$i]['dst'] = false; if ($this->check_file_size($file) && $this->check_file_type($file) && $this->check_file_ext($file)) { if ($overwrite || !file_exists($upload_dir . $file['name'])) if (move_uploaded_file($file['tmp_name'], $upload_dir . $file['name'])) { $is_ok[$i]['src'] = $file['name']; $is_ok[$i]['dst'] = $file['name']; } else { $this->error = 1; } else { $this->error = 2; } } $i++; } return $is_ok; } //把表单中存在的文件全部上传保存到$upload_dir,文件名以当前时间命名 $overwrite=1时,文件名相同,覆盖原文件 //返回上传结果数组 function save_as_timestr($upload_dir = '', $overwrite = 1) { $upload_dir = $this->set_upload_dir($upload_dir); $i = 0; $j = 0; foreach ($_FILES as $file) { $is_ok[$j]['src'] = $file['name']; $is_ok[$j]['dst'] = false; if ($this->check_file_size($file) && $this->check_file_type($file) && $this->check_file_ext($file)) { if ($overwrite || !file_exists($upload_dir . $file['name'])) { if (!$i) { list($usec) = explode(' ', microtime()); $usec = substr($usec, 2, 6); $file_name = date("ymdHis") . $usec; } $full_file_name = $file_name . "_" . $i . "." . $this->get_file_ext($file['name']); if (move_uploaded_file($file['tmp_name'], $upload_dir . $full_file_name)) { $is_ok[$j]['src'] = $file['name']; $is_ok[$j]['dst'] = $full_file_name; } else { $this->error = 1; } $i++; } else { $this->error = 2; } } $j++; } return $is_ok; } //把表单中文件域为$form_file的文件上传保存到目录$upload_dir,不改文件名 $overwrite=1时,文件名相同,覆盖原文件,返回true or false function file_save($form_file, $upload_dir = '', $overwrite = 1) { return $this->file_save_as($form_file, $_FILES[$form_file]['name'], $upload_dir, $overwrite); } //把表单中文件域为$form_file的文件上传保存到目录$upload_dir,文件名以当前时间命名 $overwrite=1时,文件名相同,覆盖原文件,返回文件名,失败返回false function file_save_as_timestr($form_file, $upload_dir = '', $overwrite = 1) { list($usec) = explode(' ', microtime()); $usec = substr($usec, 2, 6); $full_file_name = date("ymdHis") . "$usec." . $this->get_file_ext($_FILES[$form_file]['name']); if ($this->file_save_as($form_file, $full_file_name, $upload_dir, $overwrite)) return $full_file_name; else return false; } //把表单中文件域为$form_file的文件上传保存到目录$upload_dir,文件名为$file_name $overwrite=1时,文件名相同,覆盖原文件,返回true or false function file_save_as($form_file, $file_name, $upload_dir = '', $overwrite = 1) { $upload_dir = $this->set_upload_dir($upload_dir); $is_ok = false; $file = $_FILES[$form_file]; if ($this->check_file_size($file) && $this->check_file_type($file) && $this->check_file_ext($file)) { if ($overwrite || !file_exists($upload_dir . $file_name)) { if (move_uploaded_file($file['tmp_name'], $upload_dir . $file_name)) $is_ok = true; else $this->error = 1; } else { $this->error = 2; } } return $is_ok; } function set_refuse_type($refuse_type) { //设置不允许的文件类型 $this->refuse_type = $this->my_preg_replace($refuse_type); } function set_accept_type($accept_type) { //设置允许的文件类型 $this->accept_type = $this->my_preg_replace($accept_type); } function set_refuse_ext($refuse_ext) { //设置不允许的文件扩展名 $this->refuse_ext = $this->my_preg_replace($refuse_ext); } function set_accept_ext($accept_ext) { //设置允许的文件扩展名 $this->accept_ext = $this->my_preg_replace($accept_ext); } function set_max_file_size($max_file_size) { //设置单个文件大小限制(字节) $this->max_file_size = $max_file_size; } function get_error_code() {//获取最后的错误代码 return $this->error; } function get_error() { //获取最后的错误信息 switch ($this->error) { case 0: $str_err = "没有错误"; break; case 1: $str_err = "上传失败"; break; case 2: $str_err = "文件已存在"; break; case 3: $str_err = "非法的文件类型"; break; case 4: $str_err = "非法的文件扩展名"; break; case 5: $str_err = "文件大小为零或超出限制" . ($this->max_file_size / 1024) . "K"; break; case 6: $str_err = "创建目录失败"; break; } return "错误" . $this->error . ":{$str_err}。"; } function print_error() { //打印最后的错误信息 echo $this->get_error(); } function get_file_type($file_type) { //获取文件类型 $i = strrpos($file_type, "/"); if ($i > 0) return substr($file_type, 0, $i); else return ""; } function get_file_ext($file_name) { //获取文件扩展名 $i = strrpos($file_name, "."); if ($i > 0) return substr($file_name, $i + 1); else return ""; } function get_file_size($form_file) { //获取文件域对应文件大小 return $_FILES[$form_file]['size']; } function get_all_file_size($form_file) { //获取上传表单中全部文件大小 $i = 0; foreach ($_FILES as $file) { $all_file_size[$i] = $file['size']; $i++; } return $all_file_size; } function get_file_name($form_file) { //获取文件域对应文件名 return $_FILES[$form_file]['name']; } function get_all_file_name() { //获取上传表单中全部文件名 $i = 0; foreach ($_FILES as $file) { $all_file_name[$i] = $file['name']; $i++; } return $all_file_name; } //以下的protected型方法,最好不要在外部调用 function check_file_type($file) { //检查文件类型是否被允许 if ($this->accept_type != "") { if (!preg_match($this->accept_type, $this->get_file_type($file['type']))) { $this->error = 3; return false; } } if ($this->refuse_type != "") { if (preg_match($this->refuse_type, $this->get_file_type($file['type']))) { $this->error = 3; return false; } } return true; } function check_file_ext($file) { //检查文件扩展名是否被允许 if ($this->accept_ext != "") { if (!preg_match($this->accept_ext, $this->get_file_ext($file['name']))) { $this->error = 4; return false; } } if ($this->refuse_ext != "") { if (preg_match($this->refuse_ext, $this->get_file_ext($file['name']))) { $this->error = 4; return false; } } return true; } function check_file_size($file) { //检查文件大小是否超出限制 if ($file['size'] > 0) { if ($this->max_file_size > 0) { if ($file['size'] > $this->max_file_size) { $this->error = 5; return false; } } return true; } else { $this->error = 5; return false; } } function my_preg_replace($str){ //替换成正则表达式 $str = trim($str); if ($str) { $str = preg_replace("/\b/", "\b", $str); $str = "/" . $str . "/i"; } return $str; } function set_upload_dir($upload_dir) { //格式化并创建上传目录 //格式化目录 $upload_dir = preg_replace("/[\\\\\/]+/", '/', trim($upload_dir)); if ($upload_dir) { /*if (preg_match("/^\.{0,2}\//", $upload_dir)) $upload_dir = './' . $upload_dir; */ if (substr($upload_dir, -1) != '/') $upload_dir .= '/'; if (preg_match("/^[\.\/]+$/", $upload_dir)) return $upload_dir; } else if ($upload_dir === '0') { $upload_dir = './0/'; } else { return './'; } //创建目录 $arr_dir = explode('/', $upload_dir); $dir_tmp = ''; foreach ($arr_dir as $en) { $dir_tmp .= $en . '/'; if (!file_exists($dir_tmp)) { if (!@mkdir($dir_tmp)) { $this->error = 6; return false; } } } return $upload_dir; } } ?> <? //######## 测试 ######## /* if ($_FILES) { $upload = new RUpload(); print_r($upload->save_as_timestr('rupload/')); //批量上传 //print_r($upload->file_save_as_timestr('fileField1')); //单个上传 $upload->print_error(); } echo '<form enctype="multipart/form-data" method="post" action=""> <input type="file" name="fileField0" /><br /> <input type="file" name="fileField1" /><br /> <input type="file" name="fileField2" /><br /> <input type="submit" value="提交" /> </form>'; */ ?>