note 81896 added to features.file-upload.multiple
| From: | tom at nono dot be | Date: | Tue, 18 Mar 2008 15:04:25 +0000 |
| Subject: | note 81896 added to features.file-upload.multiple | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137162@lists.php.net to get a copy of this message | ||
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 benifit 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.
I hope this is a contribution...
----
Server IP: 69.147.83.197
Probable Submitter: 84.198.173.123
----
Manual Page -- http://www.php.net/manual/en/features.file-upload.multiple.php
Edit -- https://master.php.net/note/edit/81896
Del: integrated -- https://master.php.net/note/delete/81896/integrated
Del: useless -- https://master.php.net/note/delete/81896/useless
Del: bad code -- https://master.php.net/note/delete/81896/bad+code
Del: spam -- https://master.php.net/note/delete/81896/spam
Del: non-english -- https://master.php.net/note/delete/81896/non-english
Del: in docs -- https://master.php.net/note/delete/81896/in+docs
Del: other reasons-- https://master.php.net/note/delete/81896
Reject -- https://master.php.net/note/reject/81896
Search -- https://master.php.net/manage/user-notes.php