跟我學做PHP上傳類

來源:互聯網
上載者:User

up.class.php

 

<?php   class up{    public $f; //表示$_FILES[xx]    public $s; //上傳檔案的大小public $n; //是否指定檔案名稱public $d; //是否指定檔案夾 function __construct($f,$s=1,$n=false,$d=false){ $this->f=$f; $this->s=$s*1000000; $this->n=$n; $this->d=$d; $this->loading();}    function loading(){    $this->fsize();$this->ftype();$this->fmv();  }    function fname(){    if($this->n){return $this->n.$this->ftype();}else{return time().rand(111,999).$this->ftype();}  }    function fdir(){ if($this->d){  //判斷使用者是否指定檔案夾名if(!is_dir($this->d)){ //判斷指定的檔案夾名是否存在mkdir($this->d);}return $this->d."/"; }else{ if(!is_dir(date("Ymd"))){mkdir(date("Ymd"));}return date("Ymd")."/"; }  } //end fdir    function fsize(){    if($this->f['size']>$this->s){echo "檔案超過大小";exit();}else{ return true;}  }    function ftype(){ switch($this->f['type']){case "image/x-png": $ok=".png";break;case "image/png": $ok=".png";break;case "application/pdf": $ok=".pdf";break;case "image/pjpeg": $ok=".jpg";break;case "image/jpeg": $ok=".jpg";break;case "image/jpg": $ok=".jpg";break;default: $ok=false;break;}if($ok){  return $ok;}else{ echo "檔案類型不正確"; exit();}  }     function fmv(){     move_uploaded_file($this->f['tmp_name'],$this->fdir().$this->fname());   }    }  ?> test.php 上傳檔案 <?phpinclude("up.class.php"); if(!empty($_POST['sub'])){  foreach($_FILES as $id=>$v){  new up($_FILES[$id]);  } } ?><form action="" method="post" enctype="multipart/form-data"><input type="file" name="p1"><br><input type="file" name="p2"><br><input type="file" name="p3"><br><input type="file" name="p4"><br><input type="file" name="p5"><br><input type="file" name="p6"><br><input type="submit" name="sub" value="上傳"></form>  

相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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