An example of php multi-image upload code

Source: Internet
Author: User
An example of php multi-image upload code

Usage: save the above code with the file name more_upload_file.class.php. you can include this file when using it.

';} Function move_file (mvfiletmp, mvfilename) {// move the file mvfilenamearr = explode ('. ', basename (mvfilename); mvfilenamearr [0] = this-> rand_string (); mvfilename = implode ('. ', mvfilenamearr); if (is_uploaded_file (mvfiletmp) {uploadfile = self: FILE_PATH. "mvfilename"; result = move_uploaded_file (mvfiletmp, uploadfile); return result ;}} function rand_string () {string = md5 (uniqid (rand (). micr Otime (); return string;} function size_BKM (size) {// B/KB/MB unit conversion if (size <1024) {size_BKM = (string) size. "B";} elseif (size <(1024*1024) {size_BKM = number_format (double) (size/1024), 1 ). "KB";} else {size_BKM = number_format (double) (size/(1024*1024), 1 ). "MB" ;}return size_BKM ;}}?>
Share a PHP class for multi-image upload. this class of File annotation is practical and extensible. it supports multiimage Upload in gif, jpg, jpeg, pjpeg, and png formats, A rare php multi-image upload class.

A Practical PHP multi-image file Upload class that supports the multiimage Upload function in gif, jpg, jpeg, pjpeg, and png formats, you can also restrict the image type, size of the uploaded image, set the upload directory, and submit and determine some other functions. In addition, this class is not limited to image upload. you can also upload file types such as TXT and RAR, but you just need to modify the code.

For example, php full function code for multiimage Upload.

     Handledate = date ('M-d-y'); if (! Empty (file_name) {this-> file_name = file_name; this-> file_error = file_error; this-> file_size = file_size; this-> file_tmp_name = file_tmp_name; this-> file_type = file_type; this-> file_type_array = array ('/', 'image/GIF', 'image/jpg ', 'image/jpeg ', 'image/pjpeg ', 'image/png'); this-> file_type_real_array = array (0.1, 'jpg '=> 74707370, 'GIF' => 7173, 'bmp '=> 6677, 'PNG' => 807871); this-> show_execute_message (this-> file_error, this-> file_name, this-> file_type, this-> file_size) ;}} function _ destruct () {this-> file_name = NULL; this-> file_error = NULL; this-> file_size = NULL; this-> file_tmp_name = NULL; this-> file_type = NULL; self: totalsize = 0;} function show_execute_message (smfileerror, smfilename, smfiletype, smfilesize) {if (smfileerror> 0) {switch (smfileerror) {case 1: smfilemessage ='The file size exceeds the server's agreed size!'; Break; case 2: smfilemessage ='The file size exceeds the specified size!'; Break; case 3: smfilemessage ='Only part of the file is uploaded!'; Break; case 4: echo "this-> file_name".' file Upload failed!
'; Break;} self: _ destruct ();} else {smfiletypeflag = array_search (smfiletype, this-> file_type_array); // perform real format verification if (smfiletypeflag! = False) {file = fopen (this-> file_tmp_name, "rb"); bin = fread (file, 10); fclose (file ); strInfo = @ unpack ("c10chars", bin); typeCode = intval (strInfo ['chars1']. strInfo ['chars2 ']); smfiletypeflag = array_search (typeCode, this-> file_type_real_array); if (smfiletypeflag = false) {// determine whether a png image is typeCode = intval (strInfo ['chars2']. strInfo ['chars3']. strInfo ['chars4 ']); smfiletypeflag = array_search (typeCode, this-> file_type_real_array); if (smfiletypeflag = false) {// Determine whether the image is a jpg image $ typeCode = intval (strInfo ['chars7 ']. strInfo ['charss']. strInfo ['chars9']. strInfo ['chars10']); smfiletypeflag = array_search (typeCode, this-> file_type_real_array) ;}}// edit: bbs.it-home.org if ($ smfiletypeflag = false) {$ smfilemessage ='Incorrect file type. check the file!'; Self: _ destruct ();} else {$ resflag = $ this-> move_file ($ this-> file_tmp_name, this-> file_name ); if (resflag = 1) {$ smfilemessage = 'file uploaded successfully! '; Self: totalsize + = intval ($ smfilesize); self: _ destruct ();} else {$ smfilemessage ='File upload failed!'; Self: _ destruct () ;}}$ smfilesizeformat = $ this-> size_BKM (smfilesize); echo'
'. Smfilename .' '. Smfiletype .' '. Smfilesizeformat .' '. Smfilemessage .'

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.