Latif Guliyev
Latif Guliyev

Reputation: 36

How prevent $_FILES loss data

<form method="post" enctype="multipart/form-data">
    <input type="file" name="images[]">
    <input type="submit" name="submit_photo" value="SAVE">
</form>

When I choose file and after again try to choose file $_FILES keeps last version, how can I merge these versions?

Upvotes: 0

Views: 51

Answers (1)

Vinay
Vinay

Reputation: 7676

Ohh .. well that's how file control behaves even if it has multiple attribute , any following file choosing will completely replace previous selection.So the best option is to use multiple file dialog .Below is a suggestion in which we create new file controls dynamically

<form method="post" enctype="multipart/form-data" id="myfrm">
  <input type="file" name="images[0]">
  <input type="submit" name="submit_photo" value="SAVE" id="submit">
</form>

<script>
  var counter = 0;

  $('#yourmodal').on('show.bs.modal', function(){
     counter++;

     $('#myfrm').find('input[type=file]').hide(); //hide all existing file controls
     var a = '<input type="file" multiple name="resume[' + counter + ']">'; // create dynamic file control
     $('#submit').before(a); //append this to form

  });
</script>

On submitting your $_FILES array will be something like this ie. the names, types of file will be grouped together under the keys

Array
(
    [resume] => Array
        (
            [name] => Array
                (
                    [1] => Chrysanthemum.jpg
                    [2] => Hydrangeas.jpg
                )

            [type] => Array
                (
                    [1] => image/jpeg
                    [2] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [1] => C:\amp\tmp\phpF462.tmp
                    [2] => C:\amp\tmp\phpF492.tmp
                )

            [error] => Array
                (
                    [1] => 0
                    [2] => 0
                )

            [size] => Array
                (
                    [1] => 879394
                    [2] => 595284
                )

        )

)

Upvotes: 1

Related Questions