Codeigniter: Image Download

I have a large form and I just added a file so that users can upload an image.

Here's the new HTML:

<input type="file" name="file_1" />
<input type="text" name="image_description_1" class="text-input"/>

Here's the new _submit function:

if($this->CI->input->post('file_1')){
    $config['overwrite'] = TRUE;
    $config['allowed_types'] = 'jpg|jpeg|gif|png';
    $config['max_size'] = 2000;
    $config['upload_path'] = realpath(APPPATH . '../assets/uploads/avatars');

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

    $image_data = $this->CI->upload->data();

    $image['description'] = $this->CI->input->post('image_description_1');
    $image['user_id'] = $id;
    $image['image'] = $image_data['file_name'];

    $this->CI->db->insert('report_images',$image);

}

Description and user_id are sent correctly, but the file is lost.

Should I do something else? Not quite sure what is going wrong.

+3
source share
4 answers

First of all, we won’t do_upload()take it for granted, you should check upload :

if ( ! $this->upload->do_upload('file_1'))
{
    // something went wrong, display errors
}   
else
{
    // everything is fine
}

In addition, most likely this is yours upload_path, check out the document I am associated with:

//You'll need a destination folder for your uploaded images. Create a folder at the root of your CodeIgniter installation called uploads and set its file permissions to 777.  
$config['upload_path'] = './uploads/';

Therefore, you may need to change the setting to something like:

$config['upload_path'] = './assets/uploads/avatars/';
+8
source

codeigniter , userfile, , ,

$field_name = "some_field_name";
$this->upload->do_upload($field_name)
+4

I am not sure what the problem is. But maybe the links below may help you.

Error loading file in codeigniter

You can also initialize the configuration to set preferences.

$this->upload->initialize($config);

File upload class in codeigniter

+1
source

Make sure the input file name is set to "userfile" because the download function expects the files to come from the input field whose name is set to "userfile"

0
source

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


All Articles