I need to upload images, but I can not just create an input and use multiple , like this:
<input type="file" name="images[]" multiple>
What I need is to create 5 inputs that in the backend produce the same result as above.
I tried to create two inputs with the same name , but did not work:
<input type="file" name="images[]">
<input type="file" name="images[]">
PS: It has to be this way, because it makes it easier for the user to manipulate one at a time.
PHP code:
foreach(Input::file('images') as $imagem){
$destinationPath = 'uploads/' . Auth::user()->loja_id . '/produtos';
$extension = $imagem->getClientOriginalExtension();
$fileName = date('YmdHis')
.microtime(true)
.rand(111111111, 999999999)
.'.'
.$extension;
$upload_success = $imagem->move($destinationPath, $fileName);
$image = new ProdutoImagem;
$image->produto_id = $produto->id;
$image->imagem = $fileName;
$image->save();
}
AJAX code.
$(document).on('submit', '#form-produto', function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function (data){
if(data.status == true){
window.location = '/admin/' + data.url;
}
else{
$('.alert span').remove();
$('.alert').show();
$('.alert').append('<span>' + data.msg + '</span>');
}
}
});
});