Image Upload class
Solution
Class upphoto {
Public $ previewsize = 0.125; // preview the image proportion
Public $ preview = 1; // whether to generate a preview. The value is 1 and the value is 0.
Public $ datetime; // Random Number
Public $ ph_name; // upload the image file name
Public $ ph_tmp_name; // temporary Image File Name
Public $ ph_path = "uploadimg/"; // path for storing uploaded files
Public $ ph_type; // image type
Public $ ph_size; // image size
Public $ imgsize; // the size of the uploaded image, used to determine the display ratio.
Public $ al_ph_type = array ('image/jpg ', 'image/jpeg', 'image/png ', 'image/pjpeg', 'image/gif ', 'image/bmp ', 'image/xpng'); // specifies the image type that can be uploaded.
Public $ al_ph_size = 1000000; // the file size that can be uploaded
Function _ construct (){
$ This> set_datatime ();
}
Function set_datatime (){
$ This> datetime = date ("YmdHis ");
}
// Obtain the file type
Function get_ph_type ($ phtype ){
$ This> ph_type = $ phtype;
}
// Get the file size
Function get_ph_size ($ phsize ){
$ This> ph_size = $ phsize ."
";
}
// Get the upload temporary file name
Function get_ph_tmpname ($ tmp_name ){
$ This> ph_tmp_name = $ tmp_name;
$ This> imgsize = getimagesize ($ tmp_name );
}
// Obtain the original file name
Function get_ph_name ($ phname ){
$ This> ph_name = $ this> ph_path. $ this> datetime. $ phname;
}
// Determine the directory for storing uploaded files
Function check_path (){
If (! File_exists ($ this> ph_path )){
Mkdir ($ this> ph_path );
}
}
// Determine whether the size of the uploaded file exceeds the permitted size
Function check_size (){
If ($ this> ph_size> $ this> al_ph_size ){
$ This> showerror ("uploading images exceeds 2000KB ");
}
}
// Determine the file type
Function check_type (){
If (! In_array ($ this> ph_type, $ this> al_ph_type )){
$ This> showerror ("Incorrect Image Upload type ");
}
}
// Upload an image
Function up_photo (){
If (! Move_uploaded_file ($ this> ph_tmp_name, $ this> ph_name )){
$ This> showerror ("File Upload error ");
}
}
// Preview the image
Function showphoto (){
If ($ this> preview = 1 ){
When ($ 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 message
Function showerror ($ errorstr ){
Echo "";
Exit ();
}
Function save (){
$ This> check_path ();
$ This> check_size ();
$ This> check_type ();
$ This> up_photo ();
$ This> showphoto ();
}
}
I am a rookie in PHP and I write my first class. I am sorry for your shortcomings. Thank you very much for your criticism!
D8888D reply content
Test. php file
// Class instantiation:
Include ("upoop. php"); // The class file name 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 ();
// Upload the HTML of the image:
Image Source: