Pooshonk
Pooshonk

Reputation: 1324

Codeigniter $_FILES not getting all files uploaded

I have a form which allows users to add more than one file by cloning the file input. However, when I submit the form and use 'print_r($_FILES)' it doesn't show up all of the files that I have added, only the first.

HTML:

<form action="admin/admin_area/save_personnel" method="post" enctype="multipart/form-data" id="add_personnel">
<div class="certificates_list">
    <input type="file" name="user_certificates[]" />
    <input type="text" name="certificate_name[]" class="right"/>
    <input type="text" name="expiry_date[]" class="expiry_date" />
</div>
<div id="certificates_new"></div>
<p><button type="button" id="add_certificate">Add another certificate</button></p>
</form>

jQuery:

$(function() {
$('#add_certificate').click( function(event){
    event.preventDefault(); 
    var clone = $('.certificates_list:first').clone(true).appendTo('#certificates_new');
    clone.find("input").val("");
    clone.find(".expiry_date").datepicker('destroy').removeClass('hasDatepicker').removeAttr('id').datepicker({
        dateFormat: 'dd-mm-yy',  
        yearRange: '1900',
        changeMonth: true,
        changeYear: true }).attr('name', 'expiry_date[]');
     $(":input").each(function (i) { $(this).attr('tabindex', i + 1); });
});

});

If I upload 3 files and print_r($_FILES) I get the following showing only one file

Array ( [name] => Array ( [0] => license.txt ) [type] => Array ( [0] => text/plain ) [tmp_name] => Array ( [0] => C:\wamp\tmp\php42B1.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 2496 ) )

Why is it only picking up one of the files and not all of them? I have done similar stuff in the past and I haven't had any problems up until now. Any help would be greatly appreciated!

var_dump of $_FILES['user_certificates] gives me this (after I uploaded 3 files)

array (size=5)
  'name' => 
    array (size=1)
      0 => string 'license.txt' (length=11)
  'type' => 
    array (size=1)
      0 => string 'text/plain' (length=10)
  'tmp_name' => 
    array (size=1)
      0 => string 'C:\wamp\tmp\phpE11.tmp' (length=22)
  'error' => 
    array (size=1)
      0 => int 0
  'size' => 
    array (size=1)
      0 => int 2496

Upvotes: 3

Views: 2351

Answers (1)

lukeocodes
lukeocodes

Reputation: 1242

Hi I've created and tested this. Works as expected for me! :)

Document: 16562761.php

<?php
if (!empty($_POST)) {
  var_dump($_FILES);exit;
}
?>
<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  </head>
  <body>
    <form action="" method="post" enctype="multipart/form-data" id="add_personnel">
      <div id="certs_container"></div>
      <p><button type="button" id="add_certificate">Add another certificate</button></p>
      <p><button type="submit" id="submit">Submit</button></p>
    </form>
    <div style="display: none;" id="cloner">
      <div class="certificates_list">
        <input type="file" name="user_certificates[]" />
        <input type="text" name="certificate_name[]" class="right"/>
        <input type="text" name="expiry_date[]" class="expiry_date" />
      </div>
    </div>

    <script type="text/javascript">
    function add_clone() {
      var clone = $('#cloner').html();
      $('#certs_container').append(clone);
    }

    $(function() {
      add_clone();
      $('#add_certificate').click(function(e){
        add_clone();
        e.preventDefault();
      });
    });

    </script>
  </body>
</html>

Upvotes: 1

Related Questions