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.