1

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.

3
  • Multiple select should not be <input name="img[]" type="file". Its <input name="img" type="file" multiple Commented Feb 9, 2018 at 2:28
  • @DeadManAlive hello, I can’t do that. Because I also need to set a link for each image. The user needs to upload an image and a link to redirect when the image is clicked. Commented Feb 9, 2018 at 2:32
  • Its doesnt matter what you do. Its how you upload multiple files properly. once you got all the files you can do any in back-end Commented Feb 9, 2018 at 2:34

1 Answer 1

2

From the looks of your current iteration:

for($j=0; $j < $lastIndice+1; $j++){
    $imagens[$j] = $_FILES["imagens_".$j];
}

It looks like your $_FILES array is structured in this manner (example):

(
    [imagens_0] => Array
        (
            [name] => foo.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpYzdqkD
            [error] => 0
            [size] => 123
        )

    [imagens_1] => Array
        (
            [name] => bar.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpeEwEWG
            [error] => 0
            [size] => 456
        )
)

Codeigniter's do_upload() expects the key value of the $_FILES array holding your file info. Therefore by changing your for loop in this manner, you should be able to upload it:

for($j=0; $j < $lastIndice+1; $j++){
    $this->upload->do_upload("imagens_".$j);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I will try it. Thank’s!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.