php+html5+ajax實現(xiàn)上傳圖片的方法
本文實例講述了php+html5+ajax實現(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運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- php 下 html5 XHR2 + FormData + File API 上傳文件操作實例分析
- PHP使用HTML5 FileApi實現(xiàn)Ajax上傳文件功能示例
- php+html5實現(xiàn)無刷新圖片上傳教程
- 使用PHP和HTML5 FormData實現(xiàn)無刷新文件上傳教程
- php+html5使用FormData對象提交表單及上傳圖片的方法
- php 使用html5實現(xiàn)多文件上傳實例
- PHP 文件上傳進度條的兩種實現(xiàn)方法的代碼
- php實現(xiàn)簡單的上傳進度條
- php上傳文件并顯示上傳進度的方法
- PHP+Ajax無刷新帶進度條圖片上傳示例
- PHP+Ajax實現(xiàn)上傳文件進度條動態(tài)顯示進度功能
- php 使用html5 XHR2實現(xiàn)上傳文件與進度顯示功能示例
相關(guān)文章
PHP實現(xiàn)對png圖像進行縮放的方法(支持透明背景)
這篇文章主要介紹了PHP實現(xiàn)對png圖像進行縮放的方法(支持透明背景),可實現(xiàn)php針對png圖像的縮放功能,且支持透明背景,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
PHP預(yù)定義超全局?jǐn)?shù)組變量小結(jié)
這篇文章主要介紹了PHP預(yù)定義超全局?jǐn)?shù)組變量,結(jié)合實例形式總結(jié)分析了預(yù)定義超全局?jǐn)?shù)組變量的特性、功能、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2018-08-08
PHP中查詢SQL Server或Sybase時TEXT字段被截斷的解決方法
在CSDN的PHP版里老是看到有人問TEXT字段被截斷的問題,偶也回答了無數(shù)次,今天索性就總結(jié)一下吧2009-03-03

