Php Image Upload class attached call method,

Source: Internet
Author: User
Tags learn php programming

Php Image Upload class attached call method,

Examples of this article share the php Image Upload class for your reference. The specific content is as follows:

Call method:

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); // class instantiation: include ("uppoo. php "); // The class file name 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 () ;}?> // Html of the uploaded image: <form action = "upphoto. php? Action = act "method =" post "enctype =" multipart/form-data "> Image Source: <input type = "file" name = "photo"> <input type = "submit" name = "submit" value = "Upload">

Upload class. The file name to save is called uppoo. php:

<? Phpclass upphoto {public $ previewsize = 0.125; // preview image ratio public $ preview = 0; // whether to generate a preview. The value is 1, and the value is 0 public $ datetime; // random number public $ ph_name; // upload image file name public $ ph_tmp_name; // temporary image file name public $ ph_path = ". /userimg/"; // storage path of the uploaded file: public $ ph_type; // public $ ph_size of the image type; // public $ imgsize of the image size; // 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/ X-png '); // The type of the image to be uploaded is public $ al_ph_size = 1000000; // the size of the file to 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;} // obtain the file size function get_ph_size ($ phsize) {$ this-> ph_size = $ phsize. "<br>" ;}// obtain the temporary Upload File Name function get_ph_tmpname ($ tmp_name) {$ this-> ph_tmp_name = $ tmp_name; $ this-> imgsize = g Etimagesize ($ tmp_name);} // obtain the original file name function get_ph_name ($ phname) {$ this-> ph_name = $ this-> ph_path. $ this-> datetime. strrchr ($ phname ,". "); // The last occurrence location of the strrchr file point. // $ this-> ph_name = $ this-> datetime. strrchr ($ phname ,". "); // return $ this-> ph_name ;}// locate the last occurrence of the file point obtained by strrchr; // determine the Upload File storage directory 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 image exceeds 2000KB ");}} // judge the file type function check_type () {if (! In_array ($ this-> ph_type, $ this-> al_ph_type) {$ this-> showerror ("Upload image type error") ;}// upload image function up_photo () {if (! Move_uploaded_file ($ this-> ph_tmp_name, $ this-> ph_name) {$ this-> showerror ("File Upload error") ;}// image 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 message: fun Ction 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 all the content of this article. I hope it will help you learn php programming.

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.