用了一点时间,写了一个php上传图片的类,功能不太全与大家共同学习给大家分享一下,希望可以给你带来帮助.......

 以下代码命名问:upload_p_w_picpath.class.php

<?php
class upload_p_w_picpath {
    var $upload_file_name;//上传文件的名字
    var $upload_file_size;//上传文件的大小
    var $upload_max_size = 50000;//上传文件限制的大小
    var $upload__file_dir = "uploadfile/";//图片存放路径
    var $upload_tamp_dir;//上传文件的临时路径
    var $upload_file_type;//上传文件的类别
    var $upload_allow_type = array("p_w_picpath/jpeg","p_w_picpath/jpg","p_w_picpath/png","p_w_picpath/gif");//允许上传文件的类别
    var $errorstr;//错误提示
    //获取上传文件的名字
    function get_upload_name($upload_file_name){
         $type = end(explode('.',$upload_file_name));//获取图片的类型,然后重命名
         return $this->upload_file_name=$this->upload__file_dir.md5(date("ymdhis")).'.'.$type;
    }
    //获取上传文件的类型
    function get_upload_type($upload_file_type){
        return $this->upload_file_type = $upload_file_type;
    }
    //获取文件的大小
    function get_upload_size($upload_file_size){
        return $this ->upload_file_size = $upload_file_size;
    }
    //获取文件的临时目录
    function get_upload_tamp($upload_tamp_dir){
        return $this->upload_tamp_dir = $upload_tamp_dir;
    }
    //检查文件存放目录
    function check_upload_dir(){
        if(!file_exists($this ->upload__file_dir)){
              mkdir($this->upload__file_dir);
        }
    }
    //检查上传文件的大小
    function check_upload_size(){
        if($this ->upload_file_size > $this->upload_max_size){
           $this->halt("上传的文件太大");
           exit;
         }
    }
    //检查上传文件的类型
    function check_upload_type(){
        if(!in_array($this->upload_file_type,$this->upload_allow_type)){
            $this->halt("上传的文件类型错误");
            exit;
        }
    }
    //移动图片
    function uplod_p_w_picpath() {
        if(!move_uploaded_file($this->upload_tamp_dir,$this->upload_file_name)){
            $this->halt("上传文件失败");
            exit;
        }else{
            $this->halt("上传成功");
        }

    }
    //错误提醒
    function halt($errorstr){
        echo '<script>alert("'.$errorstr.'");history.go(-1);</script>';
    }
    //上传操作
    function upload(){
        $this->check_upload_type();
        $this->check_upload_size();
        $this->check_upload_dir();
        $this->uplod_p_w_picpath();
    }
}
?>

测试一下,测试代码为:test.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>


<head>


<meta http-equiv="Content-Type" content="text/html; charset=gb2312">


<title>图片上传测试</title>


</head>


<body>


<?


if($_POST['submit']){


include("upload_p_w_picpath.class.php");


$p_w_picpath = new upload_p_w_picpath();


$p_w_picpath->get_upload_name($_FILES['file']['name']);


$p_w_picpath->get_upload_size($_FILES['file']['size']);


$p_w_picpath->get_upload_type($_FILES['file']['type']);


$p_w_picpath->get_upload_tamp($_FILES['file']['tmp_name']);


$p_w_picpath->upload();


}


?>


<form action="" method="post" enctype="multipart/form-data">


<table align="center">


图片来源:<input type="file" name="file">


<input type="hidden" name="hidden" value="a">


<input type="submit" name="submit" value="上传">


</table>


</form>


</body>


</html>