CodeIgniter ajax upload image (ajax works, not CodeIgniter)

I know how to upload an ajax image in the traditional way. However, with library uploadfrom CodeIgniter I cannot figure it out.

But first let me tell you that I don't use a form at all .

if(!empty($_FILES)){

    $this->load->helper('global_helper');

    $config['upload_path']   = base_url() + "public/images/";
    $config['file_name']     = randomName();
    $config['allowed_types'] = "jpg|png|bmp";
    $config['max_size']   = '500';
    $config['overwrite']      = FALSE;

    $this->load->library('upload', $config);

    if(!$this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());
        print_r($error);
    }
}

This does not work because I get the following error:

Array
(
    [error] => <p>You did not select a file to upload.</p>
)

But I really did, and it was actually uploaded to the server, because if I print $_FILES, I get:

Array
(
    [0] => Array
        (
            [name] => no_image.png
            [type] => image/png
            [tmp_name] => C:\xampp\tmp\php1715.tmp
            [error] => 0
            [size] => 6361
        )

)

Perhaps the input problem in the array (array [0])?

The javascript code is as follows:

event.stopPropagation();
event.preventDefault();
var data = new FormData();

if(picture != undefined){ 
    $.each(picture, function (key, value) {
        data.append(key, value);
    });
}

$.ajax({
    url: 'ajax_add',
    type: 'POST',
    data: data,
    cache: false,
    processData: false,  
    contentType: false, 
    success: function(aux){
        console.log(aux);
    }
});

I do not want to use the traditional way, but if I have one too ..

Thank.

+4
source share
1

, , ,

$this->upload->do_upload(0);

, , $_FILES. $_FILES['userfile'], ( ).

, , :)

0

Source: https://habr.com/ru/post/1548515/


All Articles