2013-02-03 23 views
1

Check out this link這個PHP/IFRAME文件上傳是如何工作的?

代碼如何工作?我的意思是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> 

回答

1

你有形式操作設置爲upload.php的<form action="upload.php"因此,當您提交upload.php的知道的形式是提交(如果我正確理解你的問題,然後上面的行是真的)

+0

oops ...愚蠢的事情應該已經閱讀代碼正確...我的壞..感謝您的幫助... – ShuklaSannidhya

+1

不用擔心,有時喝咖啡休息時間是個好主意 – Shridhar

3

如果您按照HTML表單標記其相關屬性,則此代碼中的流程非常簡單。正在發生的事情是follwing:

  1. 選擇上傳
  2. 提交表單
  3. 的onsubmit()觸發JS功能,使加載..可視化和禁止上傳表單。由於*返回true *表單仍然提交。
  4. 由於'target ='屬性,表單提交被完成到iFrame中。通過該操作,您可以使iFrame源代碼「upload.php」
  5. Upload.php從iFrame中調用。
  6. Upload.php結果調用主窗口JS函數'stopUpload',通知其餘的操作。

該腳本將工作,雖然它的結果可能會改變跨瀏覽器。這是一個快速AJAX上傳實施的好腳本。

+0

我想嘗試一下,但我無法弄清楚 - 如果您添加其他文本字段,複選框等,它在哪裏說明要將表單提交給哪個文件?例如,我有一個文件「put_form_results_in_the_database.php」,我想在文件上傳後將表單結果提交給該文件。我在哪裏指定? – Natalia