php圖片上傳類 附調用方法_php技巧

來源:互聯網
上載者:User

本文執行個體為大家分享php圖片上傳類,供大家參考,具體內容如下

 調用方法:

<?phpheader("Content-Type:text/html; charset=utf-8"); //類的執行個體化:include("uppoo.php");//類的檔案名稱是upoop.php$up=newupphoto;$submit=$_POST['submit'];if($submit=="上傳"){$up->get_ph_tmpname($_FILES['photo']['tmp_name']);$up->get_ph_type($_FILES['photo']['type']);$up->get_ph_size($_FILES['photo']['size']);$up->get_ph_name($_FILES['photo']['name']);$up->save();}?>//上傳圖片的HTML:<form action="upphoto.php?action=act" method="post" enctype="multipart/form-data">圖片來源:<input type="file" name="photo"><input type="submit" name="submit" value="上傳"> 

 上傳類,儲存檔案名稱為 uppoo.php:

<?phpclass upphoto{public $previewsize=0.125 ;  //預覽圖片比例public $preview=0;  //是否產生預覽,是為1,否為0  public $datetime;  //隨機數  public $ph_name;  //上傳圖片檔案名稱  public $ph_tmp_name;  //圖片臨時檔案名稱  public $ph_path="./userimg/";  //上傳檔案存放路徑public $ph_type;  //圖片類型  public $ph_size;  //圖片大小  public $imgsize;  //上傳圖片尺寸,用於判斷顯示比例  public $al_ph_type=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png');  //允許上傳圖片類型  public $al_ph_size=1000000;  //允許上傳檔案大小 function __construct(){  $this->set_datatime(); } function set_datatime(){  $this->datetime=date("YmdHis"); }  //擷取檔案類型 function get_ph_type($phtype){   $this->ph_type=$phtype; } //擷取檔案大小 function get_ph_size($phsize){   $this->ph_size=$phsize."<br>"; } //擷取上傳臨時檔案名稱 function get_ph_tmpname($tmp_name){  $this->ph_tmp_name=$tmp_name;  $this->imgsize=getimagesize($tmp_name); } //擷取原檔案名稱 function get_ph_name($phname){  $this->ph_name=$this->ph_path.$this->datetime.strrchr($phname,"."); //strrchr擷取檔案的點最後一次出現的位置//$this->ph_name=$this->datetime.strrchr($phname,"."); //strrchr擷取檔案的點最後一次出現的位置return $this->ph_name; }// 判斷上傳檔案存放目錄 function check_path(){  if(!file_exists($this->ph_path)){   mkdir($this->ph_path);  } } //判斷上傳檔案是否超過允許大小 function check_size(){  if($this->ph_size>$this->al_ph_size){   $this->showerror("上傳圖片超過2000KB");  } } //判斷檔案類型 function check_type(){  if(!in_array($this->ph_type,$this->al_ph_type)){     $this->showerror("上傳圖片類型錯誤");  } } //上傳圖片  function up_photo(){  if(!move_uploaded_file($this->ph_tmp_name,$this->ph_name)){  $this->showerror("上傳檔案出錯");  } } //圖片預覽  function showphoto(){   if($this->preview==1){   if($this->imgsize[0]>2000){    $this->imgsize[0]=$this->imgsize[0]*$this->previewsize;       $this->imgsize[1]=$this->imgsize[1]*$this->previewsize;   }     echo("<img src=\"{$this->ph_name}\" width=\"{$this->imgsize['0']}\" height=\"{$this->imgsize['1']}\">");   }  } //錯誤提示 function showerror($errorstr){  echo "<script language=java script>alert('$errorstr');location='java script:history.go(-1);';</script>";  exit(); } function save(){  $this->check_path();  $this->check_size();  $this->check_type();  $this->up_photo();  $this->showphoto(); }}?>

以上就是本文的全部內容,希望對大家學習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.