咨询电话:023-88959644    24小时服务热线:400-023-8809
NEWS CENTER ·
新闻动态
关注中技互联 关注前沿

php中最简单的上传类代码怎么写?

发表日期:2012-02-14    文章编辑:张小川    浏览次数:9    标签:

这个是我昨晚写的一个超简单的上传类,只要 $up->upload($_FILES['imgfile']);  即可。 一条语句搞定上传
送给所有草根的个人站长

上传文件:
up.php
<?php
//验证
if(empty($_COOKIE['login'])) {
        die('index');
}
//加载类
include_once("_inc/class_uppic.php");
//上传
if(!empty($_GET['action']) && $_GET['action'] == "upload"){
        $up->upload($_FILES['imgfile']);
        echo "上传成功!文件名:{$up->new_url} <a href=?yes>重新上传</a>";
}
?>

<form action="?action=upload" method="post" enctype="multipart/form-data">
图片来源:<input type="file" name="imgfile" style="width:200px;height:25px;">
<input type="submit" name="submit" value="上传" style="width:50px;height:25px;">

//================分割线==============

类文件:
class_uppic.php
<?php
/*
        power by jtxxol.com
        2011-06-20
*/
$up=new class_uppic;
class class_uppic{
       
        function __construct(){
                //保存目录
                $this->save_path='/uploadfile/'; 
                //文件链接
                $this->save_url='/uploadfile/'; 
                //允许大小 300k
                $this->allow_size=300000;
                //允许类型
                $this->allow_ext='jpg,jpeg,gif,png';
                //新文件名
                $this->new_name=date("YmdHis").rand(10,99);
                //初始化错误提示
                $this->err='';
        }
        //上传
        function upload($arr){
                $this->file=$arr;
                //初始化设置
                $this->allow_ext=explode(',',$this->allow_ext);
                $this->save_path=$_SERVER['DOCUMENT_ROOT'].$this->save_path.date('Y-m').'/';
                $this->save_url=$this->save_url.date('Y-m').'/';
                //获得扩展名
                $temp_arr = explode(".", $this->file['name']);
                $file_ext = array_pop($temp_arr);
                $file_ext = trim(strtolower($file_ext));
                //检查类型
                if(!in_array($file_ext,$this->allow_ext)){        
                        $this->err="上传图片类型错误";        
                }
                //检查大小
                if($this->file['size']>$this->allow_size){        
                        $this->err="文件超出限制大小";        
                }
                //递归建立目录
                $this->creatdir($this->save_path);
                //上传后的路径
                $this->new_path=$this->save_path.$this->new_name.'.'.$file_ext;
                $this->new_url =$this->save_url.$this->new_name.'.'.$file_ext;
                //检查错误
                if (!empty($this->err)){
                        $this->show($this->err);
                }
                //上传
                if( !move_uploaded_file($this->file['tmp_name'],$this->new_path) ) {
                        $this->err= "上传文件出错";
                        $this->show($this->err);
                }
        }
        //建立目录
        function creatdir($Dir)
        {
                if (is_dir($Dir))
                        return true;
                if (@ mkdir($Dir,0777))
                        return true;
                if (!$this->creatdir(dirname($Dir)))
                         return false;
                return mkdir($Dir,0777);
        }
        //错误提示        
        function show($errorstr){        
                echo "<script language=javascript>alert('$errorstr');location='javascript:history.go(-1);';</script>";        
                exit();        
        }       
}        
?>

如没特殊注明,文章均为中技互联原创,转载请注明来自www.zjcoo.com
上一篇:网站优化这些误区一定要避免 百害而无一利 下一篇:已经是最后一篇了
相关新闻

CopyrightZJCOO technology Co., LTD. All Rights Reserved.    

渝ICP 备11003429号

  • qq客服
  • 公众号
  • 手机版
  • 新浪微博