当前位置:澳门新葡亰登入 > 博客热点 > 这个功能实现的效果是页面在上传文件的时候

这个功能实现的效果是页面在上传文件的时候

文章作者:博客热点 上传时间:2020-04-21

异步的公文上传是在今世的AJAX完成的Web应用里面平时要蒙受,必需清除的标题。可是正式的AJAX类不能落到实处传输文件的功力。由此,这里研讨的内容正是怎么在AJAX的本领的基本功之上营造异步的公文上传成效。在此个功用在那之中必要选取到内置的框及来传输文件。这一个效应达成的效用是页面在上传文件的时候,客商还足以使用该页面并且填写文件陈说。 那些例子是我们援引AJAX的经文案例开展剖析的。 系统环境较新本子的浏览器。比方Opera,Firefox也许 Internet Explorer。 PHP 4.3.0 或更加高版本 PHP 5 本子 PHP 中的 'short_open_tag' 选项开启(不然会生出深入解析错误卡塔尔(قطر‎。 作用分析通过嵌入的IFRAME实行理文件件上传。具有满含两个部分组成。 在页面中间有二个简易的<form...表单,表单只含有了<input type=file ... >控件。那一个表单的对象链接正是二个掩没得IFRAME(通过 CSS的品格 display: none;达成卡塔尔国并且表单里面独一多个控件的OnChange事件用来触发JavaScript函数。那个函数的意义是检查客户提交的恢宏名,然后交到表单。 在服务器端用PHP编写了三个管理进度。那几个管理进程用来把从顾客端上传的文书进行反省后保存在服务器,并且经过Javascript代码的样式重临给顾客。再次回到给顾客的Javascript脚本通过parent.window.document纠正了顾客今后正值查阅的页面,设置了文本的称号并启用了让客户提交表单的按键。启用开关的操作是经过getElementById函数达成的。 在主页面还应该有二个表单,它包含了客户提交的陈诉和隐形的文件名。客商能够在文书上传的同一时候填写文件的陈说。当文件上传截至之后,客商点击按键,就能够倾心传以往再次来到给客商的文书消息了。。 恐怕你会说那样操作不切合常理:文件在客商确认从前就曾经被交给了。即使客户并没有付诸的话,意况会怎么着呢。你能够和谐在强大管理被客户舍弃的文书。 那一个例子把文件存款和储蓄在多少个文件系统的目录下。你供给在剧本最早运转的时候配置下这一个目录,具体的含有这么些目录音讯的变量是$upload_dir 和$web_upload_dir。这里有三个对目录是还是不是可写的权力检查。 这里大家用到了以下多少个PHP函数: move_uploaded_file - 转移一经上传来服务器的文件 fopen - 展开文件 fwrite - 把内容写入文件 fclose - 关闭文件 str_replace - 替换字符串 filesize - 重回文件大小 filemtime - 重临管理时间 你能够由此手册查到那些函数假若运用。请留意要把HTM(<, >, 卡塔尔(قطر‎标志替换为(lt;, gt; 和 amp;State of Qatar. 源代码<?php$upload_dir = /var/; // 文件存款和储蓄的渠道$web_upload_dir = /aeu; // 文件在Web目录下的必须要经过的路$tf = $upload_dir.'/'.md5(rand())..test;$f = @fopen($tf, w);if ($f == false) die(Fatal error! {$upload_dir} is not writable. Set 'chmod 777 {$upload_dir}'or something like this卡塔尔;fclose($fState of Qatar;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卡塔尔国 // 未有不当 { $filename = $_FILES['file']['name']; // 文件名 move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir.'/'.$filenameState of Qatar; // 管理的主进度-转移文件到 $upload_dir $result = 'OK'; } elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE) $result_msg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; else $result_msg = 'Unknown error'; } echo '<html><head><title>-</title></head><body>'; echo '<script language=JavaScript type=text/javascript>'./n; echo 'var parDoc = window.parent.document;'; ' if ($result == 'OK') { echo 'parDoc.getElementById(upload_status).value = file successfully uploaded;'; echo 'parDoc.getElementById(filename).value = '.$filename.';'; echo 'parDoc.getElementById(filenamei).value = '.$filename.';'; echo 'parDoc.getElementById(upload_button).disabled = false;'; } else { echo 'parDoc.getElementById(upload_status).value = ERROR: '.$result_msg.';'; } echo /n.'</script></body></html>'; exit();}function safehtml($s){ $s=str_replace(, amp;, $s); $s=str_replace(<, lt;, $s); $s=str_replace(>, gt;, $s); $s=str_replace(', apos;, $s); $s=str_replace(/, quot;, $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 =<<<END <html><head><title>{$filename} [uploaded by IFRAME Async file uploader]</title></head> <body> <h1>{$filename}</h1> <p>This is a file information page for your uploaded file. Bookmark it, or send to anyone...</p> <p>Date: {$date}</p> <p>Size: {$size} bytes</p> <p>Description: <pre>{$description}</pre> </p> <p><a href={$web_upload_dir}/{$filename} style=font-size: large;>download file</a><br> <a href={$PHP_SELF} style=font-size: small;>back to file uploading</a><br> <a href={$web_upload_dir}/upload-log.html style=font-size: small;>upload-log</a></p> <br><br>Example by <a href=>AnyExample</a> </body></html> END; $f = fopen($upload_dir.'/'.$filename.'-desc.html', w); fwrite($f, $html); fclose($f); $msg = File {$filename} uploaded, <a href='{$web_upload_dir}/{$filename}-desc.html'>see file information page</a>; $f = fopen($upload_dir./upload-log.html, a); fwrite($f, <p>$msg</p>/n); fclose($f); setcookie('msg', $msg); header(Location: $_SERVER['HTTP_HOST'].$PHP_SELF); exit(); } if (isset($_COOKIE['msg']) $_COOKIE['msg'] != '') { if (get_magic_quotes_gpc()) $msg = stripslashes($_COOKIE['msg']); else $msg = $_COOKIE['msg']; setcookie('msg', ''); } ?><!-- Beginning of main page --><html><head><title>IFRAME Async file uploader example</title></head><body><?php if (isset($msg)) echo '<p style=font-weight: bold;>'.$msg.'</p>';?> <h1>Upload file:</h1><p>File will begin to upload just after selection. </p><p>You may write file description, while you file is being uploaded.</p><form action=<?=$PHP_SELF?> target=upload_iframe 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>text file uploader:</label><br> <!-- JavaScript is called by OnChange attribute --> <input type=file name=file id=file onChange=jsUpload(this)></form><script type=text/javascript>/* This function is called when user selects file in file dialog */function 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) == -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;}</script><iframe name=upload_iframe style=width: 400px; height: 100px; display: none;></iframe><!-- For debugging purposes, it's often useful to removedisplay: none from style= attribute --><br>Upload status:<br><input type=text name=upload_status id=upload_status value=not uploaded size=64 disabled><br><br>File name:<br><input type=text name=filenamei id=filenamei value=none disabled><form action=<?=$PHP_SELF?> method=POST> <!-- one field is disabled for displaying-only. Other, hidden one is for sending data --> <input type=hidden name=filename id=filename> <br><br> <label for=photo>File description:</label><br> <textarea rows=5 cols=50 name=description></textarea> <br><br> <input type=submit id=upload_button value=save file disabled></form><br><br><a href=<?=$web_upload_dir?>/upload-log.html>upload-log</a><br><br><br>Example by <a href=>AnyExample</a></body></html>

本文由澳门新葡亰登入发布于博客热点,转载请注明出处:这个功能实现的效果是页面在上传文件的时候

关键词: