php+ajax無(wú)刷新上傳圖片實(shí)例代碼
本文分享了php結(jié)合ajax實(shí)現(xiàn)無(wú)刷新上傳圖片的實(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() { //開(kāi)始上傳
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ù)庫(kù)。

希望本文所述對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。
- php+ajax實(shí)現(xiàn)圖片文件上傳功能實(shí)例
- php ajax無(wú)刷新上傳圖片實(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上傳功能集后綴名判斷和隨機(jī)命名(強(qiáng)力推薦)
本篇文章給大家分享php上傳功能集后綴名判斷和隨機(jī)命名,代碼寫的簡(jiǎn)單易懂,感興趣的朋友快來(lái)參考下吧2015-09-09
PHP7創(chuàng)建COOKIE和銷毀COOKIE的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于PHP7創(chuàng)建COOKIE和銷毀COOKIE的實(shí)例方法,有需要的朋友們可以參考下。2020-02-02
個(gè)人站長(zhǎng)制做網(wǎng)頁(yè)常用的php代碼
個(gè)人站長(zhǎng)制做網(wǎng)頁(yè)常用的php代碼...2007-03-03
php實(shí)現(xiàn)的中秋博餅游戲之繪制骰子圖案功能示例
這篇文章主要介紹了php實(shí)現(xiàn)的中秋博餅游戲之繪制骰子圖案功能,涉及php圖形繪制中位置、顏色、形狀等相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下2017-11-11

