PHP AjaxForm提交圖片上傳并顯示圖片源碼
本文實(shí)例為大家分享了PHP AjaxForm提交圖片上傳并顯示圖片的具體代碼,供大家參考,具體內(nèi)容如下
PHP dofile.php 文件上傳源碼
<?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']=="上傳"){ 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'] = "文件上傳成功!"; break; case 1: $data['status'] = 1; $data['msg'] = "文件上傳失敗,文件大小".$file_size."超過(guò)限制,允許上傳大小".sizeFormat($file_allow_size)."!"; break; case 3: $data['status'] = 1; $data['msg'] = "上傳失敗,文件只有部份上傳!"; break; case 4: $data['status'] = 1; $data['msg'] = "上傳失敗,文件沒(méi)有被上傳!"; break; case 5: $data['status'] = 1; $data['msg'] = "文件上傳失敗,文件大小為0!"; break; } if(stripos($file_allow_ext,$file_ext)===false){ $data['status'] = 1; $data['msg'] = "該文件擴(kuò)展名不允許上傳"; } if($file_size>$file_allow_size){ $data['status'] = 1; $data['msg'] = "文件大小超過(guò)限制,只能上傳".sizeFormat($file_allow_size)."的文件!"; } 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如下
<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">文件:</label><input type="file" name="file" id="file" /> <input type="submit" name="submit" value="上傳" /> </form> <script type="text/javascript"> $("#upfileimage").submit(function(){ if($("input[type=file]").val()==""){ alert("請(qǐng)選擇要上傳的文件"); return false; } }) $(function(){ var options = { type:"POST", dataType:"json", resetForm:true, success:function(o){ if(o.status==1){ alert(o.msg); }else{ $("body").append(" <img src='"+o.url+"' alt='' width='100' /><input type='hidden' name='image[]' value='"+o.url+"' />"); } }, error:function(o){ alert(o.message); } } $("#upfileimage").ajaxForm(options).submit(function(){return false;}); }) </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP+jQuery+Ajax實(shí)現(xiàn)多圖片上傳效果
- jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼
- php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
- php ajax無(wú)刷新上傳圖片實(shí)例代碼
- php+ajax實(shí)現(xiàn)異步上傳文件或圖片功能
- php+html5+ajax實(shí)現(xiàn)上傳圖片的方法
- PHP+Ajax無(wú)刷新帶進(jìn)度條圖片上傳示例
- php+ajax無(wú)刷新上傳圖片實(shí)例代碼
- PHP中ajax無(wú)刷新上傳圖片與圖片下載功能
- php+ajax實(shí)現(xiàn)帶進(jìn)度條的上傳圖片功能【附demo源碼下載】
- PHP結(jié)合jquery ajax實(shí)現(xiàn)上傳多張圖片,并限制圖片大小操作示例
相關(guān)文章
php分頁(yè)查詢的簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了php分頁(yè)查詢的簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03zend框架實(shí)現(xiàn)支持sql server的操作方法
這篇文章主要介紹了zend框架實(shí)現(xiàn)支持sql server的操作方法,結(jié)合實(shí)例形式分析了zend框架的相關(guān)代碼修改、配置文件設(shè)置與相關(guān)問(wèn)題注意事項(xiàng),需要的朋友可以參考下2016-12-12PHP使用第三方即時(shí)獲取物流動(dòng)態(tài)實(shí)例詳解
這篇文章主要介紹了PHP使用第三方即時(shí)獲取物流動(dòng)態(tài),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04weiphp微信公眾平臺(tái)授權(quán)設(shè)置
本文給大家分享的是一款開(kāi)源的微信公眾平臺(tái)weiphp的授權(quán)設(shè)置教程,非常的實(shí)用,有使用weiphp平臺(tái)的小伙伴可以參考下。2016-01-01php實(shí)現(xiàn)評(píng)論回復(fù)刪除功能
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)評(píng)論回復(fù)刪除功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05PHP實(shí)現(xiàn)簡(jiǎn)單用戶登錄界面
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)簡(jiǎn)單用戶登錄界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10php設(shè)計(jì)模式之代理模式分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之代理模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了PHP設(shè)計(jì)模式中代理模式的相關(guān)原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03