封装原生的文件上传类

<!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后台处理
<?php
header('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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值