基本思想就是把一个表单的action定位到一个隐藏的IFrame。因此,当表单提交后,结果显示在IFrame上。因为IFrame是隐藏的,所以,用户看不到。这个功能用表单的target属性来实现。
网页上的代码:
<form enctype="multipart/form-data" id="upload_files_form" target="upload_target" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<input name="upfile[]" type="file" />
<button name="submit" type="submit">点击此处上传附件</button>
<iframe id="upload_target" name="upload_target" style=" display:none;" ></iframe>
</form>
upload.php的代码:
//一个上传文件的类,可以到这里下载:A PHP Class for uploading files to server
require_once("ud_pics.php");
//添加插图
$ud = new UD_PICS(); //上面文件里类的名称
$upload = $ud->upload_pics('','');
在被调用的文件添加下面这些代码,程序执行后,会调用原页面的javascript函数stopUpload(),这样可以根据传回来的值判断是否上传成功。这里$outcome可以是0或者1,如果是字符串会出错,所以,要得到上传文件更多信息,需要取得< iframe>里接收到的值。
<script language="javascript" type="text/javascript">
window.top.window.stopUpload(<?php echo $outcome; ?>);
</script>
相关链接: