【问题标题】:How is this PHP/IFRAME file upload working?这个 PHP/IFRAME 文件上传是如何工作的?
【发布时间】:2013-01-18 06:43:24
【问题描述】:

Check out this link.

代码是如何工作的?我的意思是 IFRAME 的 src 属性设置为 # 那么upload.php 如何知道表单已提交?

这里是代码:index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Max's AJAX File Uploader</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />

<script language="javascript" type="text/javascript">
<!--
function startUpload(){
      document.getElementById('f1_upload_process').style.visibility = 'visible';
      document.getElementById('f1_upload_form').style.visibility = 'hidden';
      return true;
}

function stopUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
      document.getElementById('f1_upload_form').style.visibility = 'visible';      
      return true;   
}
//-->
</script>   
</head>

<body>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_main">Max's AJAX File Uploader</div><div id="header_right"></div></div>
            <div id="content">
                <form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
                     <p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
                     <p id="f1_upload_form" align="center"><br/>
                         <label>File:  
                              <input name="myfile" type="file" size="30" />
                         </label>
                         <label>
                             <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                         </label>
                     </p>

                     <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                 </form>
             </div>
             <div id="footer"><a href="http://www.ajaxf1.com" target="_blank">Powered by AJAX F1</a></div>
         </div>

</body>   
</html>

upload.php

<?php
   // Edit upload location here
   $destination_path = getcwd().DIRECTORY_SEPARATOR;

   $result = 0;

   $target_path = $destination_path . basename( $_FILES['myfile']['name']);

   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?>

<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result; ?>);</script>   

【问题讨论】:

    标签: php ajax iframe file-upload


    【解决方案1】:

    如果您遵循 HTML 表单标记及其相关属性,则此代码中的流程非常简单。正在发生的事情如下:

    1. 选择上传
    2. 提交表单
    3. onSubmit() 触发一个 JS 函数,该函数启用加载.. 可视化并禁用上传表单。由于 * return true* 表单仍然被提交。
    4. 由于 'target=' 属性,表单提交是在 iFrame 中完成的。通过该操作,您可以将 iFrame 源设为“upload.php”
    5. Upload.php 从 iFrame 中调用。
    6. Upload.php 结果调用主窗口 JS 函数“stopUpload”,它会通知要采取的其余操作。

    这个脚本可以工作,尽管它的结果可能会跨浏览器改变。这是一个用于快速 AJAX 上传实现的好脚本。

    【讨论】:

    • 我想试试这个,但我不知道 - 如果您添加另一个文本字段、复选框等,它会在哪里说明要将表单提交到哪个文件?例如,我有一个文件“put_form_results_in_the_database.php”,我想在文件上传后将表单结果提交到该文件。我在哪里指定?
    【解决方案2】:

    您已将表单操作设置为 upload.php &lt;form action="upload.php" 因此,当您提交 upload.php 时知道表单已提交(如果我正确理解您的问题,那么上面的行是正确的)

    【讨论】:

    • 哎呀......愚蠢的东西应该正确阅读代码......我的错......谢谢你的帮助......
    猜你喜欢
    • 2016-07-29
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多