封装原生的文件上传类

来源:互联网 发布:80端口哪个运营商 编辑:程序博客网 时间:2024/06/05 16:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><title></title></head><body><form method="post" action="rikao_info.php" enctype="multipart/form-data"><input type="file" name="filename"><input type="submit" value="上传"></form></body></html>

封装的上传类
<?php header('connect-type:text/html;charset=utf-8');    /*文件上传类*/    class Upload{        /*@param  string  上传参数*/        public function up($file){            /*获取文件参数            param  array $file  接受文件名参数 */            $filename = isset($file['name'])?$file['name']:'';            $filetype = isset($file['type'])?$file['type']:'';            $filesize = isset($file['size'])?$file['size']:'';            $fileerror = isset($file['error'])?$file['error']:'';            $filetmp = isset($file['tmp_name'])?$file['tmp_name']:'';            /* 判断文件大小*/            if($filesize > 1024*1024*2){                echo "文件过大";die();            }            /*判断文件类型*/            $type = array('image/jpg','image/gif','image/png','image/jpeg');            if(!in_array($filetype,$type)){                echo "文件格式错误";die();            }            /*判断文件错误代码 */            switch ($fileerror) {                        case 1:                           echo '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值!';                            break;                        case 2:                             echo  '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!';                            break;                        case 3:                              echo   '文件只有部分被上传!';                            break;                        case 4:                            echo '没有文件被上传!';                            break;                        case 6:                            echo '找不到临时文件夹!';                            break;                        case 7:                            echo '文件写入失败!';                            break;                    }             /*判断文件目录                 param   string    $path   文件目录路径*/             $path = './Uploads/'.date('Y').'/'.date('m').'/'.date('d').'/';             //file_exists   一个参数    判断文件是否存在             //is_dir   一个参数    检测路径是否存在            if(!file_exists($path)){                if(!mkdir($path,0777,true)){                    echo "文件目录创建失败";die();                }            }            /*移动文件:将临时文件移动到指定文件当中*/            $new_name = time().rand(0,999999999).substr($filename,strrpos($filename,'.'));                        if(move_uploaded_file($filetmp,$path.$new_name)){                return $path.$new_name;            }else{                return false;            }        }    } ?>
php后台处理
<?phpheader('content-type:text/html;charset=utf8');//include('./Smarty/Smarty.class.php');include('./DB.class.php');include('./Upload.class.php');$uploads=new Upload;$filename=isset($_FILES['filename'])?$_FILES['filename']:'';$_POST['filename']=$uploads->up($filename);$db=new Db;$res=$db->Add('pictures',$_POST);if($res){echo "添加成功";header('location:show.php');}else{    echo "添加成功";}?>



0 0
原创粉丝点击