Picture Upload class
Workaround
Class upphoto{
Public $previewsize = 0.125; Preview Picture Scale
public $preview = 1; Whether to generate a preview, is 1, no 0
Public $datetime; Random number
Public $ph _name; Upload image file name
Public $ph _tmp_name; Picture Temp file name
Public $ph _path= "uploadimg/"; Upload file storage path
Public $ph _type; Type of picture
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/' Xpng '); Allow upload of picture types
Public $al _ph_size=1000000; Allow file size to be uploaded
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 Temp 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. $phname;
}
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 ("Error uploading image type");
}
}
Upload image
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= "http://www.bkjia.c0m/phper/31/{$this >imgsize[' 0 ']}" height= "Http://www.bKjia.c0m/phper /31/{$this >imgsize[' 1 '} ">");
}
}
Error hints
function ShowError ($ERRORSTR) {
echo "";
Exit ();
}
function Save () {
$this >check_path ();
$this >check_size ();
$this >check_type ();
$this >up_photo ();
$this >showphoto ();
}
}
I am a novice php, I wrote the first class, deficiencies, welcome criticism, thank you very much!
d8888d Huitie Content
test.php file
Instantiation of the class:
Include ("upoop.php");//The file name of the class is upoop.php
$up =new Upphoto;
$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 ();
HTML for uploading images:
Picture Source:
http:/ /www.bkjia.com/phpjc/632525.html www.bkjia.com true http:// www.bkjia.com/PHPjc/632525.html techarticle Picture Upload class resolution method class upphoto{public $previewsize = 0.125;//preview picture scale public $preview = 1;//whether to generate a preview, is 1, no for 0 public $datetime; Random number ...