is it possible to make a upload with the $_FILE var using the upload library from CodeIgniter?
$this->upload->do_upload('field_name')
It requires the html field name as a parameters. But I need to make a upload with de $_FILES.
The problem is: I must be able to add more photos to a form before submitting it. I used a (+) button to clone the fields. Like this:
HTML:
...
<span onclick='duplicarCampos()'> + </span>
<div class="form-group">
<div id="destino" class="col-lg-12">
<div id="fabricas" class="col-lg-4 fabricas">
<label>Imagem:</label>
<input name="img[]" type="file" class="form-control">
<label>Link:</label>
<input name="link[]" type="text" class="form-control">
</div>
</div>
</div>
...
I'm submitting the form with AJAX, like this:
JS TO SUBMIT:
...
var imagens = $("input[name='img[]']").map(function () {
return $(this).prop("files")[0];
}).get();
var links = $("input[name='link[]']").map(function () {
return $(this).val();
}).get();
var form_data = new FormData();
for(i=0; i<imagens.length; i++){
form_data.append("imagens_"+i,imagens[i]);
form_data.append("links_"+i,links[i]);
}
form_data.append("lastIndice",(i-1));
$.ajax({
...
});
...
In my PHP Controller I receive them like this:
...
for($i=0; $i < $lastIndice+1; $i++){
$links[$i] = $_POST["links_".$i];
}
for($j=0; $j < $lastIndice+1; $j++){
$imagens[$j] = $_FILES["imagens_".$j];
}
...
Now, I wanna upload each image with the upload library from CodeIgniter. But the only way I know it works is using the html field name, not the $_FILES as a parameter of do_upload()...
USING UPLOAD LIBRARY:
...
if(!$ci->upload->do_upload($this->nome_campo)) {
...
}
...
I could make the upload "normally" with PHP, but I'm new at the CodeIgniter and I want to learn how to do things with it.
<input name="img[]" type="file". Its<input name="img" type="file" multiple