这个文件上传类可以实现多个文件或单个文件进行上传了,下面小编来给各位推荐一个不错的例子,实例代码如下:
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.phpfensi.com/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>无标题文档</title>
- </head>
-
- <body>
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class upfile {
- public $fcount = 1;
- public $ftype = array('jpg','jpeg','gif','png');
- public $fsize = 1024;
- public $fdir = 'www.111cn.net/';
- public $errormsg = '';
-
-
-
-
-
-
-
- function get_tmp_file($putfile){
- if($this->fcount == 1){
- $tmpfile = $putfile['tmp_name'];
- }else{
- for($i=0;$i<$this->fcount;$i++){
- $tmpfile[] = $putfile['tmp_name'][$i];
- }
- }
- return $tmpfile;
- }
-
-
-
-
-
-
-
- function get_suffix($filename){
- $link = pathinfo($filename);
- $suffixname = strtolower($link['extension']);
- return $suffixname;
- }
-
-
-
-
-
-
-
- function check_file_size($filesize){
- $this->errormsg = '';
- if($filesize/1000 > $this->fsize){
- $this->errormsg = '警告:文件超出大小!';
- return false;
- }else{
- return true;
- }
- }
-
-
-
-
-
-
-
- function check_file_suffix($filesuffix){
- $this->errormsg = '';
- if(!in_array($filesuffix,$this->ftype)){
- $this->errormsg = '警告:文件类型不在允许范围内!';
- return false;
- }else{
- return true;
- }
- }
-
-
-
-
-
-
-
- function move_temp_file($tmpfile,$targetfile){
- $this->errormsg = '';
- if(!move_uploaded_file($tmpfile,$targetfile)){
- $this->errormsg = '警告:文件移动失败!';
- return false;
- }else{
- return true;
- }
- }
-
-
-
-
-
-
-
-
- function update_file($putfile){
- $tmpfile = $this->get_tmp_file($putfile);
- if(!file_exists($this->fdir)){
- $this->errormsg[] = '错误:目录'.$this->fdir.'不存在';
- return $this->errormsg;
- }
- $this->fdir = substr($this->fdir,strlen($this->fdir)-1,1)=='/'?$this->fdir:$this->fdir.'/';
- if(!is_array($putfile['size'])){
- $fileinfo['file_size'] = $putfile['size'];
- if(!$this->check_file_size($fileinfo['file_size'])){
- $fileinfo['error'] = $this->errormsg;
- return $fileinfo;
- }
- $fileinfo['file_suffix'] = $this->get_suffix($putfile['name']);
- if(!$this->check_file_suffix($fileinfo['file_suffix'])){
- $fileinfo['error'] = $this->errormsg;
- return $fileinfo;
- }
-
- $fileinfo['file_name'] = date('ymdhms').'.'.$fileinfo['file_suffix'];
- if(!$this->move_temp_file($tmpfile,$this->fdir.$fileinfo['file_name'])){
- $fileinfo['error'] = $this->errormsg;
- return $fileinfo;
- }
- return $fileinfo;
-
- }else{
- for($i=0;$i<$this->fcount;$i++){
- $fileinfo[$i]['file_size'] = $putfile['size'][$i];
- if(!$this->check_file_size($fileinfo[$i]['file_size'])){
- $fileinfo[$i]['error'] = $this->errormsg;
- continue;
- }
-
- $fileinfo[$i]['file_suffix'] = $this->get_suffix($putfile['name'][$i]);
- if(!$this->check_file_suffix($fileinfo[$i]['file_suffix'])){
- $fileinfo[$i]['error'] = $this->errormsg;
- continue;
- }
-
- $fileinfo[$i]['file_name'] = date('ymdhms').rand().'.'.$fileinfo[$i]['file_suffix'];
- if(!$this->move_temp_file($tmpfile[$i],$this->fdir.$fileinfo[$i]['file_name'])){
- $fileinfo[$i]['error'] = $this->errormsg;
- continue;
- }
- }
- return $fileinfo;
- }
- }
- }
-
- ?>
- </body>
- </html>
|