I am trying out Jquery File Upload
<input type="file" name="listingfiles[]" class="file" multiple>
This upload option give many file upload option . But i want to make it only 3 to upload multiple .How i set to limit (only 3 file ) the maximum files using PHP, jQuery and AJAX. I have use 4 file
What I have tried:
HTML FILE
<div class="filediv">
<input type="file" name="listingfiles[]" class="file" multiple min="1" max="3">
</div>
JAVA CODE
if (jQuery("input[name='listingfiles[]']").length){
var lpcount = 0;
jQuery.each(jQuery("input[name='listingfiles[]']"), function(k, files) {
jQuery.each(jQuery("input[name='listingfiles[]']")[k].files, function(i, file) {
if(file.size > 1 || file.fileSize > 1) {
fd.append('listingfiles[' + lpcount + ']', file);
lpcount++;
}
});
});
}
AJAX CODE
jQuery(document).ready(function($){
'use-strict';
var abc = 1;
$(document).ready(function() {
if(jQuery('.lp-img-gall-upload-section').length > 0){
$('body').on('change', '.file', function(event) {
var files = event.target.files;
var output = document.getElementsByClassName("filediv");
output = output[0];
for(var i = 0; i< files.length; i++)
{
var file = files[i];
if(!file.type.match('image'))
continue;
var picReader = new FileReader();
picReader.addEventListener("load",function(event){
var picFile = event.target;
var div = document.createElement("ul");
div.className = 'jFiler-items-list jFiler-items-grid grid'+i;
div.innerHTML = '<li class="jFiler-item">\
<div class="jFiler-item-container">\
<div class="jFiler-item-inner">\
<div class="jFiler-item-thumb">\
<img class="thumbnail" src="'+ picFile.result +'" title="' + picFile.name + '"/>\
</div>\
</div>\
</div><a class="icon-jfi-trash jFiler-item-trash-action"></a>\
</li>';
output.insertBefore(div,null);
});
picReader.readAsDataURL(file);
}
$('.jFiler-item-trash-action').on('click',function() {
$(this).parent().parent().parent().remove();
});
$(output).find('input').hide();
$(output).before($("<div/>", {
class: 'filediv'
}).fadeIn('slow').append($("<input/>", {
name: 'listingfiles[]',
type: 'file',
class: 'file',
multiple: 'multiple'
})));
});
function imageIsLoaded(e) {
$('#previewimg' + abc).attr('src', e.target.result);
};
}
});
});
PHP CODE
if ( isset($_FILES["listingfiles"]) ) {
if($_FILES['listingfiles']['size'] != 0) {
$files = $_FILES["listingfiles"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array( 'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key] );
$_FILES = array ("listingfiles" => $file);
$count = 0;
foreach ($_FILES as $file => $array) {
if( $featImgFromGal==true && !isset($_FILES["lp-featuredimage"]) ){
$newupload = listingpro_handle_attachment($file,$postID,$set_thu=true);
}else{
$newupload = listingpro_handle_attachment($file,$postID,$set_thu=false);
}
$ids[] =$newupload;
$count++;
}
}
}
if(!empty($ids) && is_array($ids)){
$img_ids = implode(",", $ids);
update_post_meta($postID, 'gallery_image_ids', $img_ids);
}
}
}
Any suggestions on how I set the limit , 3 file only?