PHP Ajaxform Submit image upload and display image source

Source: Internet
Author: User
Tags php file upload

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*1024x768*1024x768;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 is successful!"; 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 with file size of 0!"; Break; }if(Stripos ($file _allow_ext,$file _ext)===false){$data[' status '] =1;$data[' msg '] ="The file name extension does not allow uploading"; }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<1024x768)    {return $size."bytes"; }Else if($size< (1024x768*1024x768))    {$size=round ($size/1024x768,1);return $size."KB"; }Else if($size< (1024x768*1024x768*1024x768))    {$size=round ($size/(1024x768*1024x768),1);return $size."MB"; }Else{$size=round ($size/(1024x768*1024x768*1024x768),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(){    varOptions = {type:"POST", DataType:"JSON", Resetform:true, Success: function(o){          if(o.status==1) {alert (o.msg); }Else{            $("Body"). Append ("&nbsp;&nbsp;+o.url+"' alt= ' width= '/><input type= ' hidden ' name= ' image[] ' value= '"+o.url+"'/>"); }}, Error: function(o){alert (o.message); }           }    $("#upfileimage"). Ajaxform (Options). Submit ( function(){return false;});})</script>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP Ajaxform Submit image upload and display image source

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.