php:上傳多個檔案

來源:互聯網
上載者:User

標籤:io   ar   sp   for   檔案   on   cti   bs   ad   

<?php
 class upload
 {
    public $files;
    public $seterror;
    public $allowtype;
    public $filetype;
    public $typeerror;
    public $path;
    public $succ=0;
    public $filenewname;
    function __construct($files,$path)
    {
        $this->path=$path;
        $this->files=$files;
        $this->seterror=1;
        $this->allowtype=array("jpg","gif","JPG","png","jpeg");
        $this->filetype=array();
        $this->filenewname=array();
        if(is_array($_FILES[$files][‘name‘]))
        {
            foreach($_FILES[$files][‘name‘] as $key => $names)
            {
                $file_ex=explode(".",$names);
                $this->filetype[$key]=$file_ex[count($file_ex)-1];
                $this->filenewname[$key]=$this->path.time().$key.".".$this->filetype[$key];
            }
            foreach($this->filetype as $key => $type)
            {
                if(!in_array($type,$this->allowtype))
                {
                    $this->typeerror=$key+1;
                    $this->seterror="第".$this->typeerror."張圖片類型不符合要求";
                    echo $this->seterror;
                    exit;
                }
            }
            foreach($_FILES[$files][‘error‘] as $val)
            {
                if($val!=0)
                {
                    switch($val)
                    {
                        case 1:;
                        case 2:$this->seterror="上傳的檔案過大,最大能上傳2M";break;
                        case 3:$this->seterror="檔案只有部分被上傳";break;
                        case 4:$this->seterror="檔案沒有被上傳";break;
                        case 6:$this->seterror="找不到臨時檔案夾";break;
                        case 7:$this->seterror="檔案寫入失敗";break;
                    }
                    echo $this->seterror;
                    exit;
                }
            }
            
        }
        if(!file_exists($path))
        {
            mkdir($path,0777);
        }
        if($this->seterror==1)
        {
            $this->move_file();
        }
        
    }
    private function move_file()
    {
        
        foreach($_FILES[$this->files][‘tmp_name‘] as $key => $tmp)
            {
                 if(move_uploaded_file($tmp,$this->filenewname[$key]))
                 {
                    $this->succ+=1;
                 }
            }
        if($this->succ==count($_FILES[$this->files][‘name‘]))
        {
            echo "全部上傳成功";
        }
    }
 }
?>

php:上傳多個檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.