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

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

Jun 13, 2016 am 10:38 AM
class php public upload Powerful picture of kind write

  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 of this Website
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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles