简易PHP多文件上传源码

<?php 
session_start();
header("Content-type: text/html; charset=utf-8"); 
include_once 'Upload.class.php';

upload::setVerifyBySession();
if($_POST["sbt"])
{
    $upload = new upload("pic", $_POST["verify"]);
    $picName = $upload->uploadFile();
}
?>
<html> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<head> 
<title>多文件上传</title> 

</head> 
<body> 
<a href="#" onclick="add()">增加图片上传窗</a>

<form action="" method="post" enctype="multipart/form-data" onSubmit="return btn();">
<div id="inputArea">
    <input type="file" name="pic[]" />
</div>
<input type="submit" name="sbt" value="上传" />
<input type="hidden" name="verify" value='<?php echo $_SESSION["verify"] ?>' />

</form>

<script type="text/javascript">
    function btn()
    {
        var flag = false;
        var objs = document.getElementsByName("pic[]");
        for(var i=0; i < objs.length; i++)
        {
            var obj = objs[i];
            if(obj.value != "")
            {
                flag = true;
            }
        }
        return flag;
    }

    function add()
    {
        var input = document.getElementById("inputArea");
        input.innerHTML += "<br /><input type=\"file\" name=\"pic[]\" />";
    }
</script>
</body> 
</html> 

Upload.class.php

<?php

/**
 * 图片上传类
 */
class upload
{
    private $verify;    //用于方式刷新导致重复上传的比较参数
    private $dir;        //上传目录
    private $img;        //上传的图片信息
    
    public function __construct($inputName, $verify)
    {
        session_start();
        $this->img = $_FILES[$inputName];                
        $this->verify = $verify;                        
        $this->dir = dirname(__FILE__) . "\uploads";    
    }
    
    /**
     * 设置防止重复刷新提交的比较参数Verify
     */
    public static function setVerifyBySession()
    {
        if(!isset($_SESSION["verify"]))
        {
            $_SESSION["verify"] = time();
        }
    }
    
    /**
     * 上传图片主逻辑
     */
    public function uploadFile()
    {
        self::repeatSubmit();
        self::directoryExist();
        $i = 0;
        foreach ($this->img["tmp_name"] as $tmp)
        {
            $filename = explode(".", $this->img["name"][$i++]);
            $filename = self::setName($filename);
            
            if($tmp)
            {
                $savepath = $this->dir . "\\" . $filename;
                $stats = move_uploaded_file($tmp, $savepath);
                self::showPic($stats, $filename);
            }
        }
    }
    
    /**
     * 判断是否重复提交
     */
    private function repeatSubmit()
    {
        if($this->verify != $_SESSION["verify"])
        {
            echo "<script>alert('请勿重复提交!'); window.location.href = './'</script>";
            exit();
        }
        else
        {
            unset($_SESSION["verify"]);
        }
    }
    
    /**
     * 判断上传目录是否存在
     */
    private function directoryExist()
    {
        if(!file_exists($this->dir))
        {
            mkdir($this->dir);
        }
    }
    
    /**
     * 给上传文件重新命名
     */
    private function setName($f)
    {
        $name = md5($f[0] . date("mdHis") . rand(1, time()));
        return $name . "." . $f[1];
    }
    
    /**
     * 上传成功后,显示上传图片
     */
    private function showPic($stats, $filename)
    {
        if($stats)
        {
            echo "<img src = 'uploads/$filename' />" . $filename . "<br>";
        }
    }
}
?>

 

posted @ 2013-05-06 10:31  王翔(kingfly)  阅读(768)  评论(0编辑  收藏  举报