简体   繁体   中英

Storing multiple files uploaded in different folders

I'm able to call out all my multiple files in an array and it can be able to be stored in a specific folder for eg. "./documents/".

Code for my form:
<form id="Student" name="Student" method="post" action="uploaded.php" enctype="multipart/form-data">
<input name="upload[]" id="Assign" type="file"/>
<input name="upload[]" id="Testpapers" type="file"/>
<input name="upload[]" id="others" type="file"/>
<input type="submit" name="submit" value="Submit">

Code for uploaded.php:
$number_of_uploaded_files = 0;
for ($i = 0; $i < count($_FILES['upload']['name']); $i++) 
{
 if ($_FILES['upload']['name'][$i] != '') 
{
    $dir = "documents/";
    $number_of_uploaded_files++;
    $uploaded_files[] = $_FILES['upload']['name'][$i];
    move_uploaded_file($_FILES['upload']['tmp_name'][$i], $dir. $_FILES['upload']['name'][$i])          

    }

Is it possible to store them in different folders respectively? Like store them inside "documents/assign" then "documents/testpapers". I can't think of an idea to detect array index belongs to assign or testpapers, others.

id won't pass to the server side.

But you could assign the key like below.

<input name="upload[assign]" id="Assign" type="file"/>
<input name="upload[testpapers]" id="Testpapers" type="file"/>
<input name="upload[others]" id="others" type="file"/>

In server side, you could use foreach to loop it.

foreach ($_FILES['upload']['name'] as $key => $name) {
  //...
} 

Simply change field names. Then you can acces noti via $i but through name and decide what to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM