php - dynamic upload form only uploads one file

761

A form with one upload link already defined

<form name="form1" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
Files to upload 
 <input name="file[]" type="file" multiple/><br>
    <button class="add_more">Add More Files</button>
    <input type="submit" value="Upload File" id="upload"/>
</form>

and I can add more links for more files if i wish

$(document).ready(function(){
    $('.add_more').click(function(e){
        e.preventDefault();
        $(this).before("<input name='file[]' type='file'/ multiple><br>");
    });
});

but after adding more links and files , when i process the form $_FILES array only contains the first file , from the link defined originally in the html.

the var_dump($_FILES) output is after uploading the file is

array(1) { ["file"]=> array(5) { ["name"]=> array(1) { [0]=> string(14) "xxxxxCV.doc" } ["type"]=> array(1) { [0]=> string(18) "application/msword" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phph4u15e" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(63488) } } }

Is there a way to dynamically add file uploads that end up in $_FILES ?

If i put multiple file upload lines in the html and dont use the jquery adding a link, $_FILES contains multiple files .

max_file_uploads = 20 (php.ini)

there are (to me at least) good reasons why I need to store the files in the database rather than as a link

if i create a form with 2,3,4 etc links
ie

<input name="file[]" type="file" multiple/><br>
<input name="file[]" type="file" multiple/><br>
<input name="file[]" type="file" multiple/><br>
    <button class="add_more">Add More Files</button>
    <input type="submit" value="Upload File" id="upload"/>
</form>

i can upload the files and they are in $_FILES. If i start with one link and add more with jquery, only the file uploaded from the original HTML ( not those add with jquery) is in $_FILES

People are also looking for solutions to the problem: php - Pheanstalk touch job to ask more time

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.