Multiple File Upload in PHP using IFRAME

By: Tom @ nono . be Emailed: 1699 times Printed: 2202 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Handling multiple uploads can be a lot more user friendly with a little help of javascript and form posting to iFrame... To make it all a little bit more edible, you can use AJAX and divs to provide loader gifs etc...

Work with separate forms; note the target of each form = csr. It is the ID of an iFrame somewhere on the page. I call it CSR as short for Client-Server-Request.

Also note the hidden input formId that we will use in the php upload handler.

disable submitting the form using onsubmit="return false"...

Finally a button outside the form structure starting a javascript function onclick="upload(document.form_0, document.loader_0)"

<form onsubmit="return false" id="file_0" name="file_0" action="upload.php" target="csr" enctype="multipart/form-data" method="post" style="margin:px; padding:0px">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>File 1:</td>
<td>
<input type="hidden" id="formId" name="formId" value="0" />
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" id="userFile" name="userFile" class="invulveld100pct" />
</td>
<td>
<div id="loader_0"></div>
</td>
</tr>
</table>
</form>
<form onsubmit="return false" id="file_1" name="file_1" action="upload.php" target="csr" enctype="multipart/form-data" method="post" style="margin:px; padding:0px">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>File 2:</td>
<td>
<input type="hidden" id="formId" name="formId" value="1" />
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" id="userFile" name="userFile" class="invulveld100pct" />
</td>
<td>
<div id="loader_0"></div>
</td>
</tr>
</table>
</form>
<input type="button" onclick="upload(document.form_0, document.loader_0)" />
<iframe id="csr" name="csr" height="1" width="1" style="border:0px none"></iframe>
<!--//The javascript://-->
<script type="javascript">
function upload(form, loader){
//only do this if the form exists
if(form){
//display a loadbar
loader.innerHTML = 'loading.gif';
form.submit();
}
}
</script>

The php upload handler upload.php
Remember, your page will not refresh because the post is sent to your CSR iFrame.

<?php
//there are enough examples around to handle the upload...
//the only important difference is the error reporting and the starting of the next form upload...
//presume $uploadOk is a boolean that is true if the upload succeeds; false if it fails...
//note the use of "parent" in the outputted javascript... the script is outputted into the CSR iFrame... therefor it needs parent to acces dom objects and javascript of the main page.
$currentFormId = $_POST['formId'];
$nextFormId = $_POST['formId'] + 1;

echo "<script type=\"javascript\">";

//change the content of your loader div to a desired image
if($uploadOk){
echo "parent.loader_{$currentFormId}.innerHTML = 'uploadOk.gif';";
} else {
echo "parent.loader_{$currentFormId}.innerHTML = 'uploadNotOk.gif';";
}

//submit the next form... the javascript function will only perform it if the form exists.
echo "parent.upload(document.form_{$nextFormId}, document.loader_{$nextFormId});";

echo "</script>";

?>

This is just a quick draft of how to handle multiple files this way and I'm sure you would need to optimize the workflow to fit your needs, but the benefit of working this way is although your user still chooses multiple files, they are in fact posted one by one... This means your MAX_FILE_SIZE is determined for each file separately as opposed to the combined size of all files in one post.


PHP Home | All PHP Tutorials | Latest PHP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

Handling several submissions can be a lot simple to use with a little help of javascript and type publishing to iFrame. To create it all a little bit more delicious, you can use AJAX and divs to offer loading machine gifs etc...

View Tutorial          By: Blaze Blankenship at 2012-12-05 13:04:12
2. View Comment

Managing several submissions can be a lot more simple to use with a little help of javascript and type publishing to iFrame... To create it all a little bit more edible

View Tutorial          By: Curran Cline at 2012-12-18 05:43:44

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Tom @ nono . be
Multiple File Upload in PHP using IFRAME

More Tutorials in PHP
PHP code to import from CSV file to MySQL
PHP code to write to a CSV file from MySQL query
PHP code to write to a CSV file for Microsoft Applications
Convert XML to CSV in PHP
Password must include both numeric and alphabetic characters - Magento
PHP file upload (Large Files)
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Error: Length parameter must be greater than 0
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8
Handling file locks in PHP
HTML table output using Nested for loops in PHP
Count occurrences of a character in a String in PHP

More Latest News
Most Viewed Articles (in PHP )
isset() function in PHP
Decrypting files using GnuPG (GPG) via PHP
Parent: child process exited with status 3221225477 -- Restarting
public, protected, and private Properties in PHP
A Basic Example using PHP in AWS (Amazon Web Services)
PHP code to write to a CSV file from MySQL query
Convert XML to CSV in PHP
XMLRPC for PHP - A simple client and server program
Where does the PHP run?
PHP ./configure RESULTING IN __rcp_thread_destroy@GLIBC_2_2_3_... AND UNRESOLVED REFERENCES WITH ORACLE OCI8
Convert a hex string into a 32-bit IEEE 754 float number in PHP
tmpfile() in PHP
preg_replace() and preg_replace_callback() in PHP
preg_split() and explode() in PHP
Removing Duplicate Elements from an Array in PHP
Most Emailed Articles (in PHP)
preg_split() and explode() in PHP
Traversing Arrays Using foreach in PHP
GDBM, NDBM, DB2, DB3, DBM, and CDB Databases in PHP
Sorting an Array in PHP
fixEncoding in PHP
Assignment operators in PHP
switch Statements in PHP
Renaming and Removing Files in PHP
preg_match(), function preg_match_all(), preg_grep() in PHP
Encrypting and decrypting in PHP
Setting cookies in PHP
Reading .CSV file in PHP
Encrypting files using GnuPG (GPG) via PHP
Where does the PHP run?
Setting up PHP in Windows 2003 Server IIS7, and WinXP 64