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>

5
  • Have you done a var_dump() on $_FILES to see what's actually there? Commented May 24, 2016 at 16:07
  • No,I haven't. And haven't tried var_dump before, but I'll give it a try. @EatPeanutButter Commented May 24, 2016 at 16:10
  • Did it. And I got a NULL . Wew @EatPeanutButter Commented May 24, 2016 at 16:28
  • 1
    That means your files aren't being uploaded to the server correctly. If @filippe's suggestions do not work, see this checklist of things that could be going wrong: stackoverflow.com/questions/3586919/… Commented May 24, 2016 at 16:31
  • 1
    Thanks. That link actually helped me. @EatPeanutButter Commented May 24, 2016 at 17:00

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

I would suggest using <input type="file" name="fileupload[]" multiple />, 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.
Also, check the enctype part.. you need to use multipart instead of multiple
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 :)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.