search
Homephp教程php手册php编写功能强大上传图片的类

php编写功能强大上传图片的类

Jun 13, 2016 am 10:38 AM
classphppublicuploadPowerfulpictureofkindwrite

  1.  class upphoto{
  2.   public $previewsize=0.125  ;   //预览图片比例
  3.   public $preview=0;   //是否生成预览,是为1,否为0
  4.      public $datetime;   //随机数
  5.      public $ph_name;   //上传图片文件名
  6.      public $ph_tmp_name;    //图片临时文件名
  7.      public $ph_path="../userimg/";    //上传文件存放路径
  8.   public $ph_type;   //图片类型
  9.      public $ph_size;   //图片大小
  10.      public $imgsize;   //上传图片尺寸,用于判断显示比例
  11.      public $al_ph_type=array(image/jpg,image/jpeg,image/png,image/pjpeg,image/gif,image/bmp,image/x-png);    //允许上传图片类型
  12.      public $al_ph_size=1000000;   //允许上传文件大小
  13.    function __construct(){
  14.      $this->set_datatime();
  15.    }
  16.    function set_datatime(){
  17.     $this->datetime=date("YmdHis");
  18.    }
  19.     //获取文件类型
  20.    function get_ph_type($phtype){
  21.       $this->ph_type=$phtype;
  22.    }
  23.    //获取文件大小
  24.    function get_ph_size($phsize){
  25.       $this->ph_size=$phsize."
    ";
  26.    }
  27.    //获取上传临时文件名
  28.    function get_ph_tmpname($tmp_name){
  29.      $this->ph_tmp_name=$tmp_name;
  30.      $this->imgsize=getimagesize($tmp_name);
  31.    }
  32.    //获取原文件名
  33.    function get_ph_name($phname){
  34.      $this->ph_name=$this->ph_path.$this->datetime.strrchr($phname,"."); //strrchr获取文件的点最后一次出现的位置
  35.   //$this->ph_name=$this->datetime.strrchr($phname,"."); //strrchr获取文件的点最后一次出现的位置
  36.   return $this->ph_name;
  37.    }
  38.   // 判断上传文件存放目录
  39.    function check_path(){
  40.      if(!file_exists($this->ph_path)){
  41.       mkdir($this->ph_path);
  42.      }
  43.    }
  44.    //判断上传文件是否超过允许大小
  45.    function check_size(){
  46.      if($this->ph_size>$this->al_ph_size){
  47.       $this->showerror("上传图片超过2000KB");
  48.      }
  49.    }
  50.    //判断文件类型
  51.    function check_type(){
  52.     if(!in_array($this->ph_type,$this->al_ph_type)){
  53.           $this->showerror("上传图片类型错误");
  54.     }
  55.    }
  56.    //上传图片
  57.     function up_photo(){
  58.     if(!move_uploaded_file($this->ph_tmp_name,$this->ph_name)){
  59.      $this->showerror("上传文件出错");
  60.     }
  61.    }
  62.    //图片预览
  63.     function showphoto(){
  64.        if($this->preview==1){
  65.        if($this->imgsize[0]>2000){
  66.          $this->imgsize[0]=$this->imgsize[0]*$this->previewsize;
  67.               $this->imgsize[1]=$this->imgsize[1]*$this->previewsize;
  68.        }
  69.           echo("php编写功能强大上传图片的类");
  70.       }
  71.     }
  72.    //错误提示
  73.    function showerror($errorstr){
  74.      echo "";
  75.     exit();
  76.    }
  77.    function save(){
  78.     $this->check_path();
  79.     $this->check_size();
  80.     $this->check_type();
  81.     $this->up_photo();
  82.     $this->showphoto();
  83.    }
  84.  }
  85.  ?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft