This example for you to share the PHP image upload class for your reference, the specific content as follows
Call Method:
<?php
Header ("content-type:text/html; Charset=utf-8 ");
Class is instantiated:
include ("uppoo.php");//class filename is upoop.php
$up =newupphoto;
$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 ();
>
//Upload image html:
<form action= "upphoto.php?action=act" method= "post" enctype= "Multipart/form-data" >
Photo Source: <input type= "file" name= "photo" >
<input type= "Submit" name= "submit" value= "Upload" >
Upload class, save file name called uppoo.php:
<?php class upphoto{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->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=java script>alert (' $errorstr '); Location= ' Java Script:history.go (-1); ';
</script> ";
Exit ();
function Save () {$this->check_path ();
$this->check_size ();
$this->check_type ();
$this->up_photo ();
$this->showphoto ();
}}?>
The above is the entire content of this article, I hope that you learn PHP program help.