PHP upload picture classes and usage examples _php tips

Source: Internet
Author: User
Tags datetime php class

This article describes the PHP upload image class and usage. Share to everyone for your reference, specific as follows:

1. The class file name is: upclass.php

<?php class upclass{Public $previewsize = 0.125;  Preview picture proportions Public $preview = 0;  Whether to generate a preview, is 1, no 0 public $datetime;  Random number public $ph _name;  Upload picture filename public $ph _tmp_name;  Picture Temp file name public $ph _path= "./userimg/";  Upload File Store path public $ph _type;  Picture type public $ph _size;  Picture size public $imgsize; Upload picture size, used to judge the display ratio public $al _ph_type=array (' image/jpg ', ' image/jpeg ', ' image/png ', ' image/pjpeg ', ' image/gif ', ' image  /bmp ', ' image/x-png ');  Allow upload picture type public $al _ph_size=1000000;
 Allow upload 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. "
 <br> ";
  //Get upload Temp file name function get_ph_tmpname ($tmp _name) {$this->ph_tmp_name= $tmp _name;
 $this->imgsize=getimagesize ($tmp _name); //Get the original filename function get_ph_name ($phname) {$this->ph_name= $this->ph_path. $thiS-&GT;DATETIME.STRRCHR ($phname, "."); STRRCHR gets the point where the file was last seen//$this->ph_name= $this->datetime.strrchr ($phname, ".");
 STRRCHR gets the point where the file was last seen return $this->ph_name;
  //Judge upload file to store directory function Check_path () {if (!file_exists ($this->ph_path)) {mkdir ($this->ph_path); }//To determine if the upload file exceeds the allowed size function check_size () {if ($this->ph_size> $this->al_ph_size) {$this->showerror ("Upload
  Picture more than 2000KB "); }///Judge 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->shower
  Ror ("Error uploading file"); }//Picture preview function Showphoto () {if ($this->preview==1) {if ($this->imgsize[0]>2000) {$this->imgsi
       ze[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 prompt function ShowError ($errorstr) {echo "<script language=javascript>alert (' $errorstr '); location= ' Javas
  Cript:history.go ( -1) ';</script> ';
 Exit ();
  function Save () {$this->check_path ();
  $this->check_size ();
  $this->check_type ();
  $this->up_photo ();
 $this->showphoto ();

 }}?>

2.html page:

<form action= "upphoto.php" method= "post" enctype= "Multipart/form-data" >
image Source: <input type= "file" Name= " Photo "id=" photo ">
<input type=" Submit "name=" submit "value=" Upload ">
</form>

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

<?php
Header ("content-type:text/html; Charset=utf-8 "); 
The instantiation of the class:
include ("upclass.php");//The filename 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 interested in PHP related content readers can view the site topics: "PHP file Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "Basic PHP Grammar Introduction Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.