php上傳圖片類及用法樣本_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php上傳圖片類及用法。分享給大家供大家參考,具體如下:

1.類檔案名稱為:upclass.php

<?phpclass upclass{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=javascript>alert('$errorstr');location='javascript:history.go(-1)';</script>";  exit(); } function save(){  $this->check_path();  $this->check_size();  $this->check_type();  $this->up_photo();  $this->showphoto(); }}?>

2.html頁面:

<form action="upphoto.php" method="post" enctype="multipart/form-data">圖片來源:<input type="file" name="photo" id="photo"><input type="submit" name="submit" value="上傳"></form>

3.調用類的檔案名稱為:upphoto.php

<?phpheader("Content-Type:text/html; charset=utf-8"); //類的執行個體化:include("upclass.php");//類的檔案名稱是upoop.php$up=new upclass();$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();}?><?php$images=$up->get_ph_name($_FILES['photo']['name']); echo "<img src='$images' />";?>

更多關於PHP相關內容感興趣的讀者可查看本站專題:《php檔案操作總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家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.