php+ajax無刷新上傳圖片實(shí)例代碼
本文分享了php結(jié)合ajax實(shí)現(xiàn)無刷新上傳圖片的實(shí)例代碼,分享給大家,希望大家可以和小編一起學(xué)習(xí)學(xué)習(xí),共同進(jìn)步。
1.引入文件
<!--圖片上傳begin--> <script type="text/javascript" src="/js/jquery.form.js"></script> <script type="text/javascript" src="/js/uploadImg.js"></script> <link href="/css/uploadImg.css" rel="stylesheet" type="text/css" /> <!--圖片上傳end-->
2.html部分
<div class="upimg">
<input name="icon" type="text" class="imgsrc" value="<!--{$contents.icon}-->" />
<div class="showimg">
<!--{if $contents.icon}-->
<img src="<!--{$contents.icon}-->" height="120px">
<!--{/if}-->
</div>
<div class="btn" style="height:20px;">
<span>添加圖片</span>
<input class="fileupload" type="file" name="pic[]">
</div>
</div>
3.給fileupload加上表單
/*圖片上傳*/
$(".fileupload").wrap("<form action='/bookstore/book/uploadpic' method='post' enctype='multipart/form-data'></form>"); //函數(shù)處理
4.ajax文件上傳
jQuery(function ($) {
$(".fileupload").change(function(){ //選擇文件
if ('' === $(this).val()) return;
var upimg = $(this).parent().parent().parent();
var showimg = upimg.find('.showimg');
var btn = upimg.find('.btn span');
var imgsrc = upimg.find('.imgsrc');
$(this).parent().ajaxSubmit({
//dataType: 'json', //數(shù)據(jù)格式為json
beforeSend: function() { //開始上傳
showimg.empty(); //清空顯示的圖片
imgsrc.val("");
btn.html("上傳中..."); //上傳按鈕顯示上傳中
},
uploadProgress: function(event, position, total, percentComplete) {
},
success: function(data) { //成功
//獲得后臺(tái)返回的json數(shù)據(jù),顯示文件名,大小,以及刪除按鈕
var img = data;
//顯示上傳后的圖片
imgsrc.val("");
imgsrc.val(img);
showimg.html("<img width='120' height='120' src='"+img+"'>");
btn.html("上傳成功"); //上傳按鈕還原
},
error:function(xhr){ //上傳失敗
btn.html("上傳失敗");
}
});
});
});
5.后臺(tái)進(jìn)行處理
public function uploadpicAction(){ //圖片上傳和顯示
$data = "";
$src = $this->uploadFiles2($imgpath = "/upload/book" ,$filesname = "pic");
isset($src[0]['src']) && $src[0]['src'] ? $data = $this->concaturl($src[0]['src']) : null;
echo $data;
}
6.將返回的數(shù)據(jù)交給前端,進(jìn)行一些處理。
進(jìn)而提交到后臺(tái)數(shù)據(jù)庫。

希望本文所述對大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。
- php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
- php ajax無刷新上傳圖片實(shí)例代碼
- 使用ajaxfileupload.js實(shí)現(xiàn)ajax上傳文件php版
- PHP+jQuery+Ajax實(shí)現(xiàn)多圖片上傳效果
- php+ajax實(shí)現(xiàn)異步上傳文件或圖片功能
- php+html5+ajax實(shí)現(xiàn)上傳圖片的方法
- File, FileReader 和 Ajax 文件上傳實(shí)例分析(php)
- PHP+Ajax異步帶進(jìn)度條上傳文件實(shí)例
- PHP結(jié)合jQuery插件ajaxFileUpload實(shí)現(xiàn)異步上傳文件實(shí)例
- PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
- PHP+ajax實(shí)現(xiàn)上傳、刪除、修改單張圖片及后臺(tái)處理邏輯操作詳解
相關(guān)文章
PHP面向?qū)ο蠖鄳B(tài)性實(shí)現(xiàn)方法簡單示例
這篇文章主要介紹了PHP面向?qū)ο蠖鄳B(tài)性實(shí)現(xiàn)方法,簡單說明了面向?qū)ο蠖鄳B(tài)性的原理并結(jié)合具體實(shí)例給出了php實(shí)現(xiàn)多態(tài)性的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
PHP實(shí)現(xiàn)微信JS-SDK接口選擇相冊及拍照并上傳的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)微信JS-SDK接口選擇相冊及拍照并上傳的方法,涉及php微信接口的調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
php 無法加載mysql的module的時(shí)候的配置的解決方案引發(fā)的思考
今天配置php 的時(shí)候,發(fā)現(xiàn)沒配起mysql ,wordpress提示我需要 mysql 的module之后上google搜索,大多數(shù)都是說php.ini 沒加載起2012-01-01

