0
I need to upload images, but I can’t create just one input and use the 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 it didn’t work:
<input type="file" name="images[]">
<input type="file" name="images[]">
PS: It has to be this way, because it is 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>');
}
}
});
});
What was the result of
var_dump($_FILES)
?– Woss
array(1) { ["images"]=> array(5) { ["name"]=> array(2) { [0]=> string(50) "10888874_794541107285499_31691180108787459_n.jpg" [1]=> string(17) "3geYt-profile.jpg" } ["type"]=> array(2) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" } ["tmp_name"]=> array(2) { [0]=> string(24) "C: xampp tmp php761A.tmp" [1]=> string(24) "C: xampp tmp php761B.tmp" } ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } ["size"]=> (2 array) { [0]=> int(51554) [1]=> int(49193) } } }
– Diego Vieira
If 2 images were selected, it seems to be correct. What is the problem?
– Woss
Now I put only two images to test. Of the error in the time to go through the foreach to register them.
– Diego Vieira
Then it must be the
foreach
that is wrong. See William’s answer...– Woss