首页 > 代码库 > 超简单的php上传类代码

超简单的php上传类代码

这个是我昨晚写的一个超简单的上传类,只要 $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=http://www.mamicode.com/?yes>重新上传";}?><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();                 }       }         ?>