熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php ajax無刷新上傳圖片實例代碼

2013-11-15 12:25:12  來源: PHP編程 

  將indexhtml和uploadphp文件保存到支持php的空間的同級目錄測試運行即可

  AJAX 客戶端頁面代碼 indexhtml

   <html>
<body>
<h>Ajax file upload sample</h><br/><input id="uplaod" name="btn_send" type="button" value="上傳測試"/>
<div id=result></div>
<PRE class=js name="code"><SCRIPT LANGUAGE=JavaScript>
// 上傳函數
function btn_sendonclick() {
data = ""
spliter = "dd"
datadata = data + spliter + "rn"
datadata = data + "ContentDisposition: formdata; name="photofile"; filename="C:atxt"rn"
// datadata = data + "ContentType: image/pjpeg" + vbCrLf
datadata = data + "ContentType: text/plain" + "rn" + "rn"
text = "My name is Wilson Lin"
postLength = textlength + datalength + + spliterlength +
package = data + text + "rn" + spliter + "rn"

alert(package)
// 把XML文檔發送到Web服務器
var xmlhttp = new ActiveXObject("MicrosoftXMLHTTP");
xml("POST""/uploadphp"false);
xml("ContentType" "multipart/formdata; boundary=dd");
xml("ContentLength" postLength);
xml(package);
// 顯示服務器返回的信息
resultinnerHTML=xml
}
</SCRIPT>
</PRE>
</body>
</html> PHP服務器端代碼 uploadphp

   <?php
// $_FILES[photofile]:是獲得上傳圖片的數組
// $uploadfile存放地址
$uploadfile = "D:/"$_FILES[photofile][name];
copy( $_FILES[photofile][tmp_name] $uploadfile );
echo "URL: <a http://localhost/"$_FILES[photofile][name]" target=_blank>"$_FILES[photofile][name]"</a><br/>";
?>
Upload successed!


From:http://tw.wingwit.com/Article/program/PHP/201311/20905.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.