Note that there are some explanatory texts on larger screens.

plurals
  1. POHow is this PHP/IFRAME file upload working?
    text
    copied!<p><a href="http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html" rel="nofollow">Check out this link</a>.</p> <p>How is the code working out? I mean the <code>src</code> attribute of IFRAME is set to <code>#</code> then how does <code>upload.php</code> get to know that the form was submitted?</p> <h1>Here is the code : <code>index.php</code> :</h1> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Max's AJAX File Uploader&lt;/title&gt; &lt;link href="style/style.css" rel="stylesheet" type="text/css" /&gt; &lt;script language="javascript" type="text/javascript"&gt; &lt;!-- 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 = '&lt;span class="msg"&gt;The file was uploaded successfully!&lt;\/span&gt;&lt;br/&gt;&lt;br/&gt;'; } else { result = '&lt;span class="emsg"&gt;There was an error during file upload!&lt;\/span&gt;&lt;br/&gt;&lt;br/&gt;'; } document.getElementById('f1_upload_process').style.visibility = 'hidden'; document.getElementById('f1_upload_form').innerHTML = result + '&lt;label&gt;File: &lt;input name="myfile" type="file" size="30" /&gt;&lt;\/label&gt;&lt;label&gt;&lt;input type="submit" name="submitBtn" class="sbtn" value="Upload" /&gt;&lt;\/label&gt;'; document.getElementById('f1_upload_form').style.visibility = 'visible'; return true; } //--&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt; &lt;div id="header"&gt;&lt;div id="header_left"&gt;&lt;/div&gt; &lt;div id="header_main"&gt;Max's AJAX File Uploader&lt;/div&gt;&lt;div id="header_right"&gt;&lt;/div&gt;&lt;/div&gt; &lt;div id="content"&gt; &lt;form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" &gt; &lt;p id="f1_upload_process"&gt;Loading...&lt;br/&gt;&lt;img src="loader.gif" /&gt;&lt;br/&gt;&lt;/p&gt; &lt;p id="f1_upload_form" align="center"&gt;&lt;br/&gt; &lt;label&gt;File: &lt;input name="myfile" type="file" size="30" /&gt; &lt;/label&gt; &lt;label&gt; &lt;input type="submit" name="submitBtn" class="sbtn" value="Upload" /&gt; &lt;/label&gt; &lt;/p&gt; &lt;iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"&gt;&lt;/iframe&gt; &lt;/form&gt; &lt;/div&gt; &lt;div id="footer"&gt;&lt;a href="http://www.ajaxf1.com" target="_blank"&gt;Powered by AJAX F1&lt;/a&gt;&lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <hr> <h1><code>upload.php</code> :</h1> <pre><code>&lt;?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); ?&gt; &lt;script language="javascript" type="text/javascript"&gt;window.top.window.stopUpload(&lt;?php echo $result; ?&gt;);&lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload