php+ajax無刷新上傳圖片的實(shí)現(xiàn)方法
本文實(shí)例講述了php+ajax無刷新上傳圖片的實(shí)現(xià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) { //成功
//獲得后臺返回的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.后臺進(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)而提交到后臺數(shù)據(jù)庫。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《php文件操作總結(jié)》、《PHP圖形與圖片操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP Ajax實(shí)現(xiàn)無刷新附件上傳
- php+html5實(shí)現(xiàn)無刷新圖片上傳教程
- php+ajax無刷新上傳圖片實(shí)例代碼
- ThinkPHP結(jié)合AjaxFileUploader實(shí)現(xiàn)無刷新文件上傳的方法
- 使用PHP和HTML5 FormData實(shí)現(xiàn)無刷新文件上傳教程
- php+iframe實(shí)現(xiàn)隱藏?zé)o刷新上傳文件
- php利用iframe實(shí)現(xiàn)無刷新文件上傳功能的代碼
- PHP無刷新上傳文件實(shí)現(xiàn)代碼
- php ajax無刷新上傳圖片實(shí)例代碼
- PHP+JavaScript實(shí)現(xiàn)無刷新上傳圖片
相關(guān)文章
mysql From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT日期函數(shù)
mysql日期函數(shù)From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT(后者只能格式化標(biāo)準(zhǔn)日期格式,時間戳的不行)2010-03-03
解決PHP4.0 和 PHP5.0類構(gòu)造函數(shù)的兼容問題
以下是對解決PHP4.0和PHP5.0類構(gòu)造函數(shù)兼容問題的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考一下2013-08-08
php+mysqli實(shí)現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法
這篇文章主要介紹了php+mysqli實(shí)現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法,涉及針對mysql數(shù)據(jù)庫的遍歷與表名修改等操作技巧,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12
PHP+Mysql+jQuery實(shí)現(xiàn)動態(tài)展示信息
在本文中,我將介紹如何在頁面上實(shí)現(xiàn)動態(tài)展示用戶發(fā)表的信息,將用戶發(fā)表的信息逐條播放展示。該效果可以在展示系統(tǒng)動態(tài)、商品評論等場景應(yīng)用2011-10-10

