版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、基于PHP的AJAX技術(shù)實(shí)現(xiàn)文件異步上傳本文關(guān)鍵詞: php AJAX 異步上傳 異步的文件上傳是在現(xiàn)代的AJAX實(shí)現(xiàn)的Web應(yīng)用里面經(jīng)常要遇到,必須解決的問(wèn)題。但是標(biāo)準(zhǔn)的AJAX類(XmlHttpRequest)無(wú)法實(shí)現(xiàn)傳輸 文件的功能。因此,這里討論的內(nèi)容就是如何在AJAX的技術(shù)的基礎(chǔ)之上構(gòu)建異步的文件上傳功能。在這個(gè)功能當(dāng)中需要使用到內(nèi)置的框及(IFRAME)來(lái)傳 輸文件。這個(gè)功能實(shí)現(xiàn)的效果是頁(yè)面在上傳文件的時(shí)候,用戶還可以使用該頁(yè)面并且填寫文件描述。 這個(gè)例子是我們引用AJAX的經(jīng)典案例進(jìn)行分析的。系統(tǒng)環(huán)境· 較新版本的瀏覽器。例如Opera,F(xiàn)irefox或者 Intern
2、et Explorer。 · PH 或更高版本 · PHP 5 版本· PHP 中的 'short_open_tag' 選項(xiàng)開(kāi)啟(否則會(huì)發(fā)生解析錯(cuò)誤)。功能分析通過(guò)內(nèi)置的IFRAME(框架)進(jìn)行文件上傳。具備包括三個(gè)部分組成。 · 在頁(yè)面中間有一個(gè)簡(jiǎn)單的form.表單,表單只包含了input type="file" . 控件。這個(gè)表單的目標(biāo)鏈接就是一個(gè)隱藏得IFRAME(通過(guò) CSS的風(fēng)格" display: none;"實(shí)現(xiàn))并且表單里面唯一一個(gè)控件的OnChange事件用來(lái)觸發(fā)JavaScrip
3、t函數(shù)。這個(gè)函數(shù)的作用是檢查用戶提交的擴(kuò)展名,然后提交 表單。 · 在服務(wù)器?php$upload_dir = "/var/www/anyexample/aeu" / 文件存儲(chǔ)的路徑$web_upload_dir = "/aeu" / 文件在Web目錄下的路徑$tf = $upload_dir.'/'.md5(rand().".test"$f = fopen($tf, "w");if ($f = false) die("Fatal error! is not writable.
4、Set 'chmod 777 'or something like this");fclose($f);unlink($tf);/處理上傳的文件if (isset($_POST'fileframe') $result = 'ERROR'$result_msg = 'No FILE field found'if (isset($_FILES'file') / 從瀏覽器接受文件if ($_FILES'file''error' = UPLOAD_ERR_OK) / 沒(méi)有錯(cuò)誤$fi
5、lename = $_FILES'file''name' / 文件名 move_uploaded_file($_FILES'file''tmp_name', $upload_dir.'/'.$filename);/ 處理的主過(guò)程-轉(zhuǎn)移文件到 $upload_dir $result = 'OK'elseif ($_FILES'file''error' = UPLOAD_ERR_INI_SIZE)$result_msg = 'The uploaded file e
6、xceeds the upload_max_filesize directive in php.ini'else $result_msg = 'Unknown error'echo 'htmlheadtitle-/title/headbody'echo 'script language="JavaScript" type="text/javascript"'."n"echo 'var parDoc = window.parent.document;''if
7、 ($result = 'OK')echo 'parDoc.getElementById("upload_status").value = "file successfully uploaded"'echo 'parDoc.getElementById("filename").value = "'.$filename.'"'echo 'parDoc.getElementById("filenamei").value = &q
8、uot;'.$filename.'"'echo 'parDoc.getElementById("upload_button").disabled = false;'elseecho 'parDoc.getElementById("upload_status").value = "ERROR: '.$result_msg.'"'echo "n".'/script/body/html'exit();function saf
9、ehtml($s)$s=str_replace("&", "&", $s);$s=str_replace("", "<", $s);$s=str_replace("", ">", $s);$s=str_replace("'", "'", $s);$s=str_replace(""", """,
10、 $s);return $s;if (isset($_POST'description')$filename = $_POST'filename'$size = filesize($upload_dir.'/'.$filename);$date = date('r', filemtime($upload_dir.'/'.$filename);$description = safehtml($_POST'description');$html =ENDhtmlheadtitle uploaded by
11、 IFRAME Async file uploader/title/headbodyh1/h1pThis is a file information page for your uploaded file. Bookmark it, or send to anyone./ppDate: /ppSize: bytes/ppDescription: pre/pre/ppa href="/" style="font-size: large;"download file/abra href="" style="font-size:
12、small;"back to file uploading/abra href="/upload-log.html" style="font-size: small;"upload-log/a/pbrbrExample by a href="/body/htmlEND;$f = fopen($upload_dir.'/'.$filename.'-desc.html', "w");fwrite($f, $html);fclose($f);$msg = "File upload
13、ed, a href='/-desc.html'see file information page/a"$f = fopen($upload_dir."/upload-log.html", "a");fwrite($f, "p$msg/pn");fclose($f);setcookie('msg', $msg); header("Location: http:/".$_SERVER'HTTP_HOST'.$PHP_SELF); exit(); if (iss
14、et($_COOKIE'msg') && $_COOKIE'msg' != '') if (get_magic_quotes_gpc() $msg = stripslashes($_COOKIE'msg'); else$msg = $_COOKIE'msg'setcookie('msg', ''); ?!- Beginning of main page -htmlheadtitleIFRAME Async file uploader example/title/hea
15、dbody?php if (isset($msg) echo 'p style="font-weight: bold;"'.$msg.'/p'? h1Upload file:/h1pFile will begin to upload just after selection. /ppYou may write file description, while you file is being uploaded./pform action="?=$PHP_SELF?" target="upload_iframe&q
16、uot; method="post" enctype="multipart/form-data"input type="hidden" name="fileframe" value="true"!- Target of the form is set to hidden iframe -!- From will send its post data to fileframe section of this PHP script (see above) -label for="file&
17、quot;text file uploader:/labelbr!- JavaScript is called by OnChange attribute -input type="file" name="file" id="file" onChange="jsUpload(this)"/formscript type="text/javascript"/* This function is called when user selects file in file dialog */funct
18、ion jsUpload(upload_field)/ this is just an example of checking file extensions/ if you do not need extension checking, remove / everything down to line/ upload_field.form.submit();var re_text = /.txt|.xml|.zip/i;var filename = upload_field.value;/* Checking file type */if (filename.search(re_text)
19、= -1)alert("File does not have text(txt, xml, zip) extension");upload_field.form.reset();return false;upload_field.form.submit();document.getElementById('upload_status').value = "uploading file."upload_field.disabled = true;return true;/scriptiframe name="upload_ifra
20、me" style="width: 400px; height: 100px; display: none;"/iframe!- For debugging purposes, it's often useful to remove"display: none" from style="" attribute -brUpload status:brinput type="text" name="upload_status" id="upload_status" value="not uploaded" size="64" disabledbrbrFile name:brinput type="text" name="filenamei" id=&
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026上半年貴州事業(yè)單位聯(lián)考玉屏侗族自治縣招聘41人備考題庫(kù)有答案詳解
- 初級(jí)社工考試題庫(kù)及答案
- 測(cè)量理論考試試卷及答案
- 頸椎骨折選擇試題及答案
- 2025-2026人教版二年級(jí)數(shù)學(xué)上期末卷
- 2025-2026五年級(jí)信息技術(shù)期末測(cè)試粵教版
- 腸道菌群與代謝病線粒體功能障礙
- 腸道-腦軸在麻醉藥品依賴性評(píng)價(jià)中的意義
- 肝血管瘤臨床路徑變異的觀察策略
- 探店汽修店衛(wèi)生管理制度
- 2026 年初中英語(yǔ)《狀語(yǔ)從句》專項(xiàng)練習(xí)與答案 (100 題)
- 2026年遼寧省盤錦市高職單招語(yǔ)文真題及參考答案
- 簡(jiǎn)愛(ài)插圖本(英)夏洛蒂·勃朗特著宋兆霖譯
- 焊接專業(yè)人才培養(yǎng)方案
- 第二屆全國(guó)技能大賽江蘇省選拔賽焊接項(xiàng)目評(píng)分表
- 糖尿病護(hù)士年終總結(jié)
- 第20課 《美麗的小興安嶺》 三年級(jí)語(yǔ)文上冊(cè)同步課件(統(tǒng)編版)
- 糖尿病基礎(chǔ)知識(shí)培訓(xùn)2
- 研學(xué)旅行概論第六章
- GB/T 22176-2023二甲戊靈乳油
- 根據(jù)信用證制作商業(yè)發(fā)票、裝箱單、裝船通知
評(píng)論
0/150
提交評(píng)論