views:

63

answers:

5

All the scripts I have found via Google search rely on some sort of screen that first asks the user how many files they need to upload and the upload script works based on that number.

But what if I don't want any limits, but want to allow the user to upload as many or as few as they want. How to go about this?

All I have for now is the form:

<form enctype="multipart/form-data" method="post" action="script.php">
 <input id="my_file_element" type="file" name="myFile[]" >
        <input type="submit" value="Upload!" name="submit">
</form>

EDIT

I think the way I phrased my question is causing some misunderstanding. I am not in need of some way to add more "input type = file" field elements. I am already using a javascript that enables the user to add more form fields by clicking a button, those other fields are identical to the 1st one posted above.

What I am asking this question for, is to know how to handle the actual upload of files to the server using a php script.

A: 

Just use JavaScript to append another input element to the form.

Aaron Harun
please see my edit
Timmy
+1  A: 

EDIT

That depends on how you called the file input elements. If you followed my approach below, you can get the number of files with:

if (empty($_FILES['attach']['name']))
    $count_files = 0;
else
    $count_files = count($_FILES['attach']['name']);

Try var_dump($_FILES) to see the complete $_FILES array, in case you have any doubts on its structure.

ORIGINAL

Something like this, with Javascript:

<script type="text/javascript">
var j = 2; //default two files
function novoa() {
    j = j+1;
    var elem = document.createElement("input");
    var par = document.getElementById("filed");
    elem.setAttribute("type", "file");
    elem.name = "attach[]";
    elem.id = "file_" + j;
    par.appendChild(elem);
}
function delult() {
    if (j == 0) return;
    var elem = document.getElementById("file_" + j);
    var par = document.getElementById("filed");
    j = j-1;
    par.removeChild(elem);
}
</script>
...
<p id="addupl"><a href="javascript:novoa()">More files</a> |
    <a href="javascript:delult()">Less files</a></p>
<p id="filed">
<input type="file" name="attach[]" id="file_1" />
<input type="file" name="attach[]" id="file_2" />
</p>
Artefacto
sorry for you having to go through all that trouble, please see my edit.
Timmy
+2  A: 

EDIT

Let's say your form is like this

<form enctype="multipart/form-data" method="post" action="script.php">
 <input id="my_file_element" type="file" name="myFile[]" >
 <input id="my_file_element" type="file" name="myFile[]" >
 <input id="my_file_element" type="file" name="myFile[]" >
 etc...
<input type="submit" value="Upload!" name="submit">
</form>

What you want to do is get all the files called myFile[]. Here's what you do in PHP:

foreach ($_FILES["myFile"]["error"] as $key => $error) {
 if ($error == UPLOAD_ERR_OK) {
   $tmp_name = $_FILES["myFile"]["tmp_name"][$key];
   $name = $_FILES["myFile"]["name"][$key];
   // here we move it to a directory called data
   // you can move it wherever you want
   move_uploaded_file($tmp_name, "data/$name");
 } else {
   // an error occurred, handle it here
 }
}

ORIGINAL

Using HTML and JavaScript alone, there isn't any easy way to upload multiple files at the same time.

I would use a Flash library, such as SWFUpload, to do the upload instead. Using this plugin, you can show a standard multi-file upload window, as shown in this demo. This will allow users to easily select multiple files in the same file browser.

If you don't want to use Flash for some reason, you could use JavaScript to dynamically swap input elements, as in this example.

Dumb Guy
A: 

OMG you're asking how to handle this form?
there is an example right on the manual page:
http://www.php.net/manual/en/features.file-upload.post-method.php

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "data/$name");
    }
}
?>
Col. Shrapnel
A: 

In your PHP script, do a var_dump($_FILES). The output should help you move further into the direction you wish. Take a look at PHP's filesystem functions for more info on what you can do with the files, after using the move_uploaded_file function.

In order to really answer your question How to handle the upload, you'd need to tell us what you want to accomplish/do with the files.

Lauri Lehtinen