Dynamic file upload

hi everyone!
i’m thinking of dong file upload function…

basic idea is that user can enter an integer in a text box…that integer represents how many file he/she wants to upload…for example, user enters 3.
when user clicks “UPLOAD FILE NOW” button, a table with 3 rows displayed…one row for each file to upload…
when user clicks “FINISH” button, details of files uploaded will be displayed…

i just don’t know how to do upload function with array…
is it possible?

Kalon, thanks for responding…

i’m a newbie in php…if i may ask, what is “$key => $value” used for?

noted! :slight_smile:

this is the code for my upload file function…

foreach($_FILES[‘file’][‘name’] as $key => $value) {

$target = “budgetdetail/”;
$target = $target . basename( $_FILES[‘file’][‘name’][$key]);

$fileName=($_FILES[‘file’][‘name’][$key]);

if (file_exists($target . $_FILES[‘file’][‘name’][$key]))
{
?> <script type=“text/javascript”>
alert(“Error
File already exists”);
</script>
<?php
}
else
{
move_uploaded_file($_FILES[‘file’][‘tmp_name’][$key], $target);
$fileName = addslashes($fileName);
mysql_query(“INSERT INTO budgetdetail (fileArea,fileName) VALUES (‘$target’,‘$fileName’)”) or die(mysql_error());
}

}

is there anything wrong? because, when i run the code, alert box with “File already exists” message always popup…

i want to move the uploaded file into “budgetdetail” folder, and save it’s directory and file name into MySQL…

$key and $value are just variables to store the current array index and current value at that index as you loop through an array performing tasks on each array element using a foreach() loop.

$key and $value can be called anything (except reserved words)

To upload multiple files in 1 go you will need something like this

 
<input type="file" name="txtUploadFilepath[]" />

and in your server side script

 
foreach($_FILES['txtUploadFilepath']['name'] as $key => $value) {
      
       $uploadedFile = $_FILES['txtUploadFilepath']['name'][$key];

      if(is_uploaded_file($_FILES['txtUploadFilepath']['tmp_name'][$key]))   {
             move_uploaded_file($_FILES['txtUploadFilepath']['tmp_name'][$key], $uploadedFile));

      }

}

You should also add some error checking.