PHP upload Image class and usage example

Source: Internet
Author: User
Tags getting started with php php programming
This article describes the PHP upload image class and usage. Share to everyone for your reference, as follows:

1. class file name is: upclass.php

<?phpclass upclass{public $previewsize = 0.125;  Preview Picture scale public $preview = 0;  Whether to generate a preview, is 1, no 0 public $datetime;  Random number public $ph _name;  Upload picture file name public $ph _tmp_name;  Picture temporary file name public $ph _path= "./userimg/";  Upload file storage path public $ph _type;  Picture type public $ph _size;  Picture size public $imgsize; Upload image size for judging the display scale public $al _ph_type=array (' image/jpg ', ' image/jpeg ', ' image/png ', ' image/pjpeg ', ' image/gif ', ' image  /bmp ', ' image/x-png ');  Allow uploading of picture types public $al _ph_size=1000000; Allow upload of file size function __construct () {$this->set_datatime ();} function Set_datatime () {$this->datetime=date (" Ymdhis "); }//Get file type function Get_ph_type ($phtype) {$this->ph_type= $phtype;}//Get file Size function get_ph_size ($phsize) {$this- >ph_size= $phsize. "
"; } Get upload Temporary file name function get_ph_tmpname ($tmp _name) {$this->ph_tmp_name= $tmp _name; $this->imgsize=getimagesize ($tmp _name); }//Get the original file name function Get_ph_name ($phname) {$this->ph_name= $this->ph_path. $this->datetime.strrchr ($phname, " ."); STRRCHR gets the location of the last occurrence of the point of the file//$this->ph_name= $this->datetime.strrchr ($phname, "."); STRRCHR gets the point where the file last occurred return $this->ph_name; }//determine the upload file storage directory function Check_path () {if (!file_exists ($this->ph_path)) {mkdir ($this->ph_path); }}//Determine if the upload file exceeds the allowable size function check_size () {if ($this->ph_size> $this->al_ph_size) {$this->showerror (" Upload images over 2000KB "); }}//Determine file type function Check_type () {if (!in_array ($this->ph_type, $this->al_ph_type)) {$this->showerror ("Upload picture Type error "); }}//Upload picture function Up_photo () {if (!move_uploaded_file ($this->ph_tmp_name, $this->ph_name)) {$this- ShowError ("Error uploading file"); }}//Picture preview 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 ("Ph_name}\" width=\ "{$this->imgsize[' 0 ']}\" height=\ "{$this->imgsize[' 1 ']}\" > "); }}//Error function ShowError ($ERRORSTR) {echo ""; Exit (); } function Save () {$this->check_path (); $this->check_size (); $this->check_type (); $this->up_photo (); $this->showphoto (); }}?>

2.html page:

3. The file name of the calling class is: upphoto.php

<?phpheader ("content-type:text/html; Charset=utf-8 "); Class instantiation: Include ("upclass.php");//The file name of the class is Upoop.php$up=new upclass (); $submit =$_post[' Submit '];if ($submit = = "Upload") {$ 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 "";? >

More about PHP related content readers can view the topic: "PHP File Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document tips summary (including word, excel,access,ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

The above describes the PHP upload image class and usage examples, including uploading images, PHP content, I hope that the PHP tutorial interested in a friend helpful.

  • Large-Scale Price Reduction
    • 59% Max. and 23% Avg.
    • Price Reduction for Core Products
    • Price Reduction in Multiple Regions
    undefined. /
    Connect with us on Discord
    • Secure, anonymous group chat without disturbance
    • Stay updated on campaigns, new products, and more
    • Support for all your questions
    undefined. /
    Free Tier
    • Start free from ECS to Big Data
    • Get Started in 3 Simple Steps
    • Try ECS t5 1C1G
    undefined. /

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.