I have a multiple upload function using json, on my view each file that has a error should display the file upload error message.
I can see that if I upload 3 images and all 3 images have errors only 2 of the messages will display on the view.
I use ajax to submit my form.
How can I make sure that if all 3 images would display error messages for all 3 rather than just 2 on view.
Ajax Script
Code:
<script type="text/javascript">
$('#button-upload').on('click', function() {
$('#upload-form').remove();
$('body').prepend('<form enctype="multipart/form-data" id="upload-form" style="display: none;"><input type="file" name="userfile[]" value="" multiple="multiple" /></form>');
$('#upload-form input[name=\'userfile[]\']').trigger('click');
if (typeof timer != 'undefined') {
clearInterval(timer);
}
timer = setInterval(function() {
if ($('#upload-form input[name=\'userfile[]\']').val() != '') {
clearInterval(timer);
$.ajax({
url: 'index.php?c=filemanager&m=upload<?php echo $directory;?>',
type: 'post',
dataType: 'json',
data: new FormData($('#upload-form')[0]),
cache: false,
contentType: false,
processData: false,
success: function(json) {
if (json['error']) {
$('#messages').append('<div class="alert alert-danger">' + json['error'] + '</div>');
}
if (json['success']) {
$('#messages').append('<div class="alert alert-success">' + json['success'] + '</div>');
$('.refresh').trigger('click');
$(this).tooltip('hide');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
}, 1000);
});
</script>
Function
PHP Code:
public function upload() {
$json = array();
if (!$json) {
$files = $_FILES;
if ($files) {
$check_if_any_error = 0;
$fieldname = 'userfile';
$cpt = count($_FILES[$fieldname]['name']);
for($i=0; $i<$cpt; $i++) {
$_FILES[$fieldname]['name']= $files[$fieldname]['name'][$i];
$_FILES[$fieldname]['type']= $files[$fieldname]['type'][$i];
$_FILES[$fieldname]['tmp_name']= $files[$fieldname]['tmp_name'][$i];
$_FILES[$fieldname]['error']= $files[$fieldname]['error'][$i];
$_FILES[$fieldname]['size']= $files[$fieldname]['size'][$i];
$url = '';
$directory = $this->input->get('image_directory');
if (isset($directory)) {
$url .= $directory . '/';
} else {
$url .= '';
}
$config['upload_path'] = FCPATH . 'images/catalog/' . $url;
$config['allowed_types'] = 'gif|png';
$config['max_size'] = 10000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname)) {
$check_if_any_error++;
}
}
if ($check_if_any_error > 0 ) {
$json['error'] = $this->upload->display_errors();
} else {
$json['success'] = 'You have upload your selected files!';
}
}
}
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($json));
}
There's only one rule - please don't tell anyone to go and read the manual. Sometimes the manual just SUCKS!