PHP AjaxForm submits the Image Upload and displays the image source code,
The example in this article shares with you the specific code for submitting an Image Upload and displaying the image in PHP AjaxForm for your reference. The specific content is as follows:
PHP dofile. php file upload source code
<? 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'] = "the file is uploaded successfully! "; Break; case 1: $ data ['status'] = 1; $ data ['msg '] =" File Upload Failed, file size ". $ file_size. "The upload size exceeds the limit ". sizeFormat ($ file_allow_size ). "! "; Break; case 3: $ data ['status'] = 1; $ data ['msg '] =" Upload Failed. only part of the file is uploaded! "; Break; case 4: $ data ['status'] = 1; $ data ['msg '] =" Upload Failed, file not uploaded! "; Break; case 5: $ data ['status'] = 1; $ data ['msg '] =" An error occurred while uploading the file. The file size is 0! "; Break;} if (stripos ($ file_allow_ext, $ file_ext) ===false) {$ data ['status'] = 1; $ data ['msg '] = "this file extension cannot be uploaded";} if ($ file_size> $ file_allow_size) {$ data ['status'] = 1; $ data ['msg '] = "the file size exceeds the limit and can only be uploaded ". sizeFormat ($ file_allow_size ). "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 (random, 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/, 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" ;}}?>
The HTML is 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 ("select the 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 = 'ddden 'name = 'image [] 'value = '" + o. url + "'/>") ;}}, error: function (o) {alert (o. message) ;}}$ ("# upfileimage "). ajaxForm (options ). submit (function () {return false ;}) </script>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.