1

I want to upload images for adding property and have some code in which i am able to add more than 5 file uploads. How can stop the uploads at five. here is the sample code i am using

<form enctype="multipart/form-data" action="" method="post">
<div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>

and the js code for add button is

$('#add_more').click(function() {
$(this).before($("<div/>", {id: 'filediv'}).fadeIn('slow').append(
$("<input/>", {name: 'file[]', type: 'file', id: 'file'}),        
$("<br/><br/>")
));
});

how can i stop add more button can be available for only five.

chaitanya
  • 352
  • 4
  • 14
  • You are creating up to 5 div's all with the same `id` thats not legal HTML and is likely to cause JQuery an issue. You need to change the `id` to `class` and then its legal. Of course you also need to change your CSS as well – RiggsFolly Aug 04 '15 at 12:35
  • var count = 1; $('#add_more').click(function() { if(count >=5){ $(this).before($("
    ", {id: 'filediv'}).fadeIn('slow').append( $("", {name: 'file[]', type: 'file', id: 'file'}), $("

    ") )); }else{ alert("Can not add more then 5") } count++; });
    – Dharmendra Aug 04 '15 at 12:37
  • @Dharmendra Maybe that would be better as an answer then we could actually read it. You may also get points for it if its right – RiggsFolly Aug 04 '15 at 12:38
  • @RiggsFolly i added a code because he can think about the logic . – Dharmendra Aug 04 '15 at 12:40
  • possible duplicate of [jQuery count number of divs with a certain class?](http://stackoverflow.com/questions/7404544/jquery-count-number-of-divs-with-a-certain-class) – RiggsFolly Aug 04 '15 at 12:40
  • @Dharmendra I have no problem, it woudl be better as an answer than a comment is all I was saying. You can then format it so its readable to questioner and anyone that searches and find this question at a later time – RiggsFolly Aug 04 '15 at 12:41

3 Answers3

1

Before adding the input, check if there are 5 in de div with id filediv. So yes, stop with return.

I've changed and simplified your code, so the input fields will be added and count in one div (div#filediv).

$('#add_more').click(function() {
   if ($('div#filediv').find('input[type="file"]').length >= 5)
      return;
   $('div#filediv').append(
      $('<input/>', {name: 'file[]', type: 'file', id: 'mustBeUniquePerPage'})
   );
});

Make sure that you use unique id's per page!

More efficient solution, less DOM and memory intensive:

var totalFilesAdded = 0;
$('#add_more').on('click', function() {
   if (totalFilesAdded >= 5)
      return;
   $('div#filediv').append(
      $('<input/>', {name: 'file[]', type: 'file', id: 'mustBeUniquePerPage'})
   );
   totalFilesAdded++;
});

Read here more why you better use .on('click') vs .click(): Difference between .on('click') vs .click()

Community
  • 1
  • 1
schellingerht
  • 5,726
  • 2
  • 28
  • 56
  • Except that you should only have on thing in an HTML page with a specific `id` multiple with same `id` is likely to cause JQuery an issue – RiggsFolly Aug 04 '15 at 12:37
  • @RiggsFolly It's an append to the div, isn't it? – schellingerht Aug 04 '15 at 12:39
  • No see the .before, he is using the same `{id: 'filediv'}` in all the adds of a new input field It shoudl really be a `class="filediv"` In the base HTML and the Addition js – RiggsFolly Aug 04 '15 at 12:44
1

The most important consideration here is that you can only really restrict the number of file uploads on the server. No Javascript or HTML will ever prevent users uploading more than 5 files. You need to set limits on the server.

The max_file_uploads PHP.ini directive allows you to limit the number of files sent via a POST request to the PHP file processing uploads. Even this will only restrict how many files are uploaded in a single POST request. You also need to track in your database/file system how many have been uploaded already and check these limits are not breached.

The name attribute you have give to your input element (file[]) implies the use of a multiple upload facility, but your HTML does not have the multiple attribute. You can either have a multiple file upload in which users can upload all 5 images at once, or keep doing it as you are and check how many input fields have been generated (I won't post code for this as another answer already has).

Either way please remember that you will still need to check how many files have already been uploaded, as everything in HTML and Javascript can be bypassed.

HenryTK
  • 1,287
  • 8
  • 11
1

You can use global variable as a counter and initialize it to 0. On click increment the counter. Before adding any property just check if count is less than 5.

var counter = 0;
$('#add_more').click(function() {
counter = counter+1;
if(counter<=5){
$(this).before(...)
}
)};
Abhilasha
  • 1,177
  • 4
  • 10
  • 17