PHP Upload Image

Source: Internet
Author: User

Upload page

 
<? PHP // class instantiation: Include ("IMG. PHP "); // The class file name is upoop. PHP $ up = new upphoto; $ 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: --> <form action = "image. php? Action = act "method =" Post "enctype =" multipart/form-Data "> Image Source: <input type = "file" name = "photo"> <input type = "Submit" name = "Submit" value = "Upload"> </form>

IMG. 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 ', 'ima Ge/X-PNG '); // The image type that can be uploaded is public $ al_ph_size = 1000000; // function _ construct () {$ this-> set_datatime ();} function set_datatime () {$ this-> datetime = date ("ymdhis");} // get 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-> imgsiz E = getimagesize ($ 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 () ;}}?>

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.