Php ajaxform submits the Image Upload and displays the image source code,

Source: Internet
Author: User
Tags php file upload

Php ajaxform submits the Image Upload and displays the image source code,

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 ("& nbsp;  <input type = 'ddden 'name = 'image [] 'value = '" + o. url + "'/>") ;}}, error: function (o) {alert (o. message) ;}}$ ("# upfileimage "). ajaxForm (options ). submit (function () {return false ;}) </script>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.