PHP ajaxform Submit picture upload and display picture

Source: Internet
Author: User
This article is mainly for everyone to introduce the PHP ajaxform submission image upload and display image source code, with a certain reference value, interested in small partners can refer to

PHP dofile.php File Upload source

<?php$file_upload = "upload/"; $file _allow_ext= ' gif|jpg|jpeg|png|gif|zip|rar|ppt|xls|pdf|pptx|xlsx|docx '; $file _allow_size = 5*1024*1024;if ($_post[' Submit ']== "upload") {if (Is_uploaded_file ($_files[' file ' [' Tmp_name '])) {$file _  Name = $_files[' file ' [' Name '];  $file _error = $_files[' file ' [' Error '];  $file _type = $_files[' file ' [' type '];   $file _tmp_name = $_files[' file ' [' Tmp_name '];  $file _size = $_files[' file ' [' Size ']; $file _ext = substr ($file _name, Strrpos ($file _name, '. ')  +1);  Switch ($file _error) {case 0: $data [' status '] = 0;    $data [' msg '] = "File upload succeeded!";  Break  Case 1: $data [' status '] = 1; $data [' msg '] = "File upload failed, file size". $file _size. "    Over limit, allow upload size ". Sizeformat ($file _allow_size)."! ";  Break  Case 3: $data [' status '] = 1;    $data [' msg '] = "Upload failed, file only partial upload!";  Break  Case 4: $data [' status '] = 1;    $data [' msg '] = "Upload failed, File not uploaded!";  Break  Case 5: $data [' status '] = 1;    $data [' msg '] = "File upload failed, file size is 0!";   Break } if (Stripos ($file _allow_ext, $file _ext) ===false) {$data [' status '] =1;    $data [' msg '] = "The file extension is not allowed to be uploaded";  if ($file _size> $file _allow_size) {$data [' status '] = 1; $data [' msg '] = "file size exceeds limit and can only be uploaded". Sizeformat ($file _allow_size). "   The file! ";}  if ($data [' status ']==1) {$data [' status '] = 1;  $data [' msg '] = $data [' msg '];   Exit (Json_encode ($data)); if ($data [' status ']==0) {if (file_exists ($file _upload)) {$file _new_name = date ("Ymdhis"). ' _ '. Rand (10000,99999). '.   $file _ext;    $file _save_path = $file _upload. $file _new_name;     $data [' status '] = 0;   $data [' url '] = $file _save_path;   Move_uploaded_file ($file _tmp_name, $file _save_path);      Exit (Json_encode ($data));  }else{exit (Json_encode ($data)); }}}}function Sizeformat ($size) {$sizeStr = "; if ($size <1024) {return $size." Bytes ";  } else if ($size < (1024*1024)) {$size =round ($size/1024,1); return $size. " KB ";  } else if ($size < (1024*1024*1024)) {$size =round ($size/(1024*1024), 1); return $size. " MB ";  } else {$size =round ($size/(1024*1024*1024), 1); return $size. " GB "; }}?>

HTML as follows

<script type= "Text/javascript" src= "Http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js" ></ Script><script type= "Text/javascript" src= "Http://files.cnblogs.com/files/china-li/jquery.form.js" >< /script><form action= "dofile.php" method= "post" enctype= "Multipart/form-data" id= "Upfileimage" ><input Type= "hidden" name= "image[]"/><label for= "File" > File: </label><input type= "file" name= "file" id= " File "/><input type=" Submit "name=" submit "value=" upload "/></form><script type=" Text/javascript ">$  ("#upfileimage"). Submit (function () {if ($ ("input[type=file]"). val () = = "") {alert ("Please select a file to upload"); return false; }}) $ (function () {var options = {type: "POST", DataType: "JSON", Resetform:true, Success:function (o) {if (o.status==1   ) {alert (o.msg); }else{$ ("Body"). Append ("<input] type= ' hidden ' name= ' image[' value=   ' "+o.url+" '/> "); }}, Error:function (o) {alert (O.messaGE); }} $ ("#upfileimage"). Ajaxform (Options). Submit (function () {return false;});}) </script>

The above is the whole content of this article, I hope that everyone's study has helped.


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.