2

I'm trying to insert multiple images in database per transaction, but each image has its own input types. But when I submit the form, I get the error that my $file_upload = $_FILES['file_upload'.$i]; from my post.php is an "undefined index" . Are the file-upload1, file-upload2, file-upload3 from my HTML are not the correct way to do this? Please help. Thank you.

My php code is:

include 'dbcontroller.php';
if(isset($_POST['submit'])) {
    for ($i = 1; $i <= 3; $i++) {
        $file_upload = $_FILES['file_upload'.$i];

        $file=(rand(1000,100000)."-".$file_upload['name'.$i]);
        $type=$file_upload['type'.$i];
        $size=$file_upload['size'.$i];
        $loc=$file_upload['tmp_name'.$i];

        $new_size=$size/1024; // file size in KB

        // make file name in lower case
        $new_file_name = strtolower($file);
        // make file name in lower case

        $final_file=str_replace(' ','-',$new_file_name);

        if(move_uploaded_file($loc, '..admin/officers-avatars/'.$final_file)) {
            $result = mysqli_query($conn,"INSERT INTO images VALUES ('$final_file', '$new_size', '$type')")
            or die(mysqli_error($conn));
        }
    }
}

Below is my HTML

<form action="post.php" method="post" enctype="multiple/form-data">
 <input type="file" name="file-upload1" /><br><br>
 <input type="file" name="file-upload2" /><br><br>
 <input type="file" name="file-upload3" /><br><br>

 <input type="submit" name="submit" value="SAVE"/>
</form>
Louie
  • 219
  • 4
  • 13

1 Answers1

2

You have a typo: Change enctype="multiple/form-data" for enctype="multipart/form-data".

Anyway, I would suggest you to use an array in name attribute instead:

<form action="post.php" method="post" enctype="multipart/form-data">
    <input type="file" name="fileupload[]" /><br><br>
    <input type="file" name="fileupload[]" /><br><br>
    <input type="file" name="fileupload[]" /><br><br>

    <input type="submit" name="submit" value="SAVE"/>
</form>

Then in PHP you use this:

foreach($_FILES['fileupload'] as $file) {
    ....
}

Or you can use multiple:

<input type="file" name="fileupload[]" multiple /><br><br>
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
  • I would suggest using ``, but be careful what you allow your users to upload directly into your database, you could get some harmful viruses uploaded onto your site and not know how or where they came from. – Jonathan Yaniv Ben Avraham May 24 '16 at 16:14
  • Also, check the enctype part.. you need to use `multipart` instead of `multiple` – Felippe Duarte May 24 '16 at 16:19
  • i tried your code but i'm having an error. `Notice: Undefined index: fileupload` and `Warning: Invalid argument supplied for foreach() ` both on the same line. and thanks for noticing my typo error :) – Louie May 24 '16 at 16:26
  • I've found the exact error I had, it was just only that my file's name in my HTML is not the same with my php that's why I'm getting an undefined error. Fixed it and working now. Your answer was useful tho, thanks. – Louie May 24 '16 at 16:59