php+html5+ajax實(shí)現(xiàn)上傳圖片的方法
本文實(shí)例講述了php+html5+ajax實(shí)現(xiàn)上傳圖片的方法。分享給大家供大家參考,具體如下:
<?php if (isset($_POST['upload'])) { var_dump($_FILES); move_uploaded_file($_FILES['upfile']['tmp_name'], 'up_tmp/'.time().'.dat'); //header('location: test.php'); exit; } ?>
<!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <title>HTML5 Ajax Uploader</title> <script src="jquery-2.1.1.min.js"></script> </head> <body> <p><input type="file" id="upfile"></p> <p><input type="button" id="upJS" value="用原生JS上傳"></p> <p><input type="button" id="upJQuery" value="用jQuery上傳"></p> <script> /*原生JS版*/ document.getElementById("upJS").onclick = function() { /* FormData 是表單數(shù)據(jù)類 */ var fd = new FormData(); var ajax = new XMLHttpRequest(); fd.append("upload", 1); /* 把文件添加到表單里 */ fd.append("upfile", document.getElementById("upfile").files[0]); ajax.open("post", "test.php", true); ajax.onload = function () { console.log(ajax.responseText); }; ajax.send(fd); } /* jQuery 版 */ $('#upJQuery').on('click', function() { var fd = new FormData(); fd.append("upload", 1); fd.append("upfile", $("#upfile").get(0).files[0]); $.ajax({ url: "test.php", type: "POST", processData: false, contentType: false, data: fd, success: function(d) { console.log(d); } }); }); </script> </body> </html>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php 下 html5 XHR2 + FormData + File API 上傳文件操作實(shí)例分析
- PHP使用HTML5 FileApi實(shí)現(xiàn)Ajax上傳文件功能示例
- php+html5實(shí)現(xiàn)無(wú)刷新圖片上傳教程
- 使用PHP和HTML5 FormData實(shí)現(xiàn)無(wú)刷新文件上傳教程
- php+html5使用FormData對(duì)象提交表單及上傳圖片的方法
- php 使用html5實(shí)現(xiàn)多文件上傳實(shí)例
- PHP 文件上傳進(jìn)度條的兩種實(shí)現(xiàn)方法的代碼
- php實(shí)現(xiàn)簡(jiǎn)單的上傳進(jìn)度條
- php上傳文件并顯示上傳進(jìn)度的方法
- PHP+Ajax無(wú)刷新帶進(jìn)度條圖片上傳示例
- PHP+Ajax實(shí)現(xiàn)上傳文件進(jìn)度條動(dòng)態(tài)顯示進(jìn)度功能
- php 使用html5 XHR2實(shí)現(xiàn)上傳文件與進(jìn)度顯示功能示例
相關(guān)文章
php策略模式簡(jiǎn)單示例分析【區(qū)別于工廠模式】
這篇文章主要介紹了php策略模式,對(duì)比工廠模式簡(jiǎn)單分析了php策略模式的原理與使用方法,需要的朋友可以參考下2019-09-09PHP實(shí)現(xiàn)的簡(jiǎn)單異常處理類示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單異常處理類,結(jié)合具體實(shí)例形式分析了php基于面向?qū)ο蠹夹g(shù)實(shí)現(xiàn)異常處理操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05PHP實(shí)現(xiàn)對(duì)png圖像進(jìn)行縮放的方法(支持透明背景)
這篇文章主要介紹了PHP實(shí)現(xiàn)對(duì)png圖像進(jìn)行縮放的方法(支持透明背景),可實(shí)現(xiàn)php針對(duì)png圖像的縮放功能,且支持透明背景,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07PHP預(yù)定義超全局?jǐn)?shù)組變量小結(jié)
這篇文章主要介紹了PHP預(yù)定義超全局?jǐn)?shù)組變量,結(jié)合實(shí)例形式總結(jié)分析了預(yù)定義超全局?jǐn)?shù)組變量的特性、功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-08-08PHP中查詢SQL Server或Sybase時(shí)TEXT字段被截?cái)嗟慕鉀Q方法
在CSDN的PHP版里老是看到有人問TEXT字段被截?cái)嗟膯栴},偶也回答了無(wú)數(shù)次,今天索性就總結(jié)一下吧2009-03-03