版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、基于PHP的AJAX技术实现文件异步上传本文关键词: php AJAX 异步上传 异步的文件上传是在现代的AJAX实现的Web应用里面经常要遇到,必须解决的问题。但是标准的AJAX类(XmlHttpRequest)无法实现传输 文件的功能。因此,这里讨论的内容就是如何在AJAX的技术的基础之上构建异步的文件上传功能。在这个功能当中需要使用到内置的框及(IFRAME)来传 输文件。这个功能实现的效果是页面在上传文件的时候,用户还可以使用该页面并且填写文件描述。 这个例子是我们引用AJAX的经典案例进行分析的。系统环境· 较新版本的浏览器。例如Opera,Firefox或者 Intern
2、et Explorer。 · PH 或更高版本 · PHP 5 版本· PHP 中的 'short_open_tag' 选项开启(否则会发生解析错误)。功能分析通过内置的IFRAME(框架)进行文件上传。具备包括三个部分组成。 · 在页面中间有一个简单的form.表单,表单只包含了input type="file" . 控件。这个表单的目标链接就是一个隐藏得IFRAME(通过 CSS的风格" display: none;"实现)并且表单里面唯一一个控件的OnChange事件用来触发JavaScrip
3、t函数。这个函数的作用是检查用户提交的扩展名,然后提交 表单。 · 在服务器?php$upload_dir = "/var/www/anyexample/aeu" / 文件存储的路径$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) / 没有错误$fi
5、lename = $_FILES'file''name' / 文件名 move_uploaded_file($_FILES'file''tmp_name', $upload_dir.'/'.$filename);/ 处理的主过程-转移文件到 $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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国腊油项目投资可行性研究报告
- 企业固定资产管理模板全覆盖
- 快装90°弯头行业深度研究报告
- 财务管理基础报表制作模板集
- 中国万向接头项目投资可行性研究报告
- 中国车库门窗项目投资可行性研究报告
- 中国鸡熟肉项目投资可行性研究报告
- 2025简易店铺转让合同协议书
- 卷门机行业深度研究报告
- 中国VF80变频器项目投资可行性研究报告
- 5G通信模块设计项目分析方案
- 汽车维修入股协议合同
- 教育家精神课件
- 2026蒙牛「星动力」校园招聘笔试考试参考试题及答案解析
- 2026湖南省气象部门招聘应届毕业生59人(第2601号)笔试考试参考试题及答案解析
- 垃圾填埋场施工流程方案
- 2025年实验室检验质控知识考试试题及答案解析
- 2025年宿州市纪委市委巡察办所属事业单位选调工作人员8人考试参考试题及答案解析
- 2025贵州毕节市中级人民法院招聘聘用制法官助理30人考试参考题库及答案解析
- 2025及未来5年中国电子记事本市场调查、数据监测研究报告
- 医院地震疏散培训
评论
0/150
提交评论