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ù)類(lèi) */
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)容感興趣的讀者可查看本站專(zhuān)題:《php文件操作總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(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 mysql數(shù)據(jù)庫(kù)操作分頁(yè)類(lèi)
如果您加了新功能,或者是有改進(jìn),請(qǐng)與大家一起分享。2008-06-06
在PHP中養(yǎng)成7個(gè)面向?qū)ο蟮暮昧?xí)慣
如果您尚未打算用 OO 原則創(chuàng)建應(yīng)用程序,則使用 PHP 的面向?qū)ο螅∣O)的語(yǔ)言特性,這 7 個(gè)習(xí)慣將幫助您開(kāi)始在過(guò)程編程與 OO 編程之間進(jìn)行轉(zhuǎn)換。2010-07-07
簡(jiǎn)介WordPress中用于獲取首頁(yè)和站點(diǎn)鏈接的PHP函數(shù)
這篇文章主要介紹了WordPress中用于獲取首頁(yè)和站點(diǎn)鏈接的PHP函數(shù),分別是home_url()和site_url()需要的朋友可以參考下2015-12-12
基于PHP實(shí)現(xiàn)用戶登錄注冊(cè)功能的詳細(xì)教程
這篇文章主要介紹了基于PHP實(shí)現(xiàn)用戶登錄注冊(cè)功能的詳細(xì)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

