CesarMiguel
CesarMiguel

Reputation: 3830

Codeigniter upload file and save name with special caracters

I'm saving images in a folder to be sent by the user. But if i save image with special characters (ç, á, ã, etc.), the system does not recognize, and save the file name wrong.

Example: file name: cação.png and save: caà § à £ o

Controller:

public function save_image(){
    $config['upload_path'] = 'images/uploaded/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '300';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

    $this->load->library('upload', $config);
    $sub_data = array(
        'error' => '',
        'result' => ''
    );

    if( ! $this->upload->do_upload())
    {
        $sub_data['error'] = "* Erro a carregar a imagem, por favor verifique todos os requisitos da imagem.<br/>
            Error type: ";
        $sub_data['error'] .= $this->upload->display_errors();
    }
    else 
    {
        $sub_data['result'] = $this->upload->data();
        $name_file = $sub_data['result']['file_name'];

        $data['logotipo'] = $sub_data['result']['file_name'];
        $this->edit_data_m->update_image($data);

    }
    $template_data['main_content'] = $this->load->view('editar_imagem_empresa_v', $sub_data, true);
    $this->load->view('template_admin_v', $template_data);
}

Model:

public function update_image($data){
    $this->load->helper('file');

    $imagem = $this->get_image();
    $nome_img = $this->get_image()->result_array();
    if($imagem->num_rows() > 0 && $nome_img[0]['logotipo'] != "sem_logotipo.png")
    {
        $imagem2 = $this->get_image()->result_array();
        unlink('./images/uploaded/'.$imagem2[0]['logotipo']);
    }
    $this->db->where('id', $this->session->userdata('id_empresa'));
    $this->db->update('empresa_tbl', $data);
}

Thanks for your help!

Upvotes: 2

Views: 2800

Answers (2)

pc_oc
pc_oc

Reputation: 545

Controller:

    if( ! $this->upload->do_upload())
    {
        $sub_data['error'] = "* Erro a carregar a imagem, por favor verifique todos os requisitos da imagem.<br/>
            Tipo de erro: ";
        $sub_data['error'] .= $this->upload->display_errors();
    }
    else 
    {
        $sub_data['result'] = $this->upload->data();
        $name_file = preg_replace('/[^(\x20-\x7F)]*/','', $sub_data['result']['file_name']);
        $data['logotipo'] = $sub_data['result']['file_name'];
        $this->edit_data_m->update_image($data);
    }
    $template_data['main_content'] = $this->load->view('editar_imagem_empresa_v', $sub_data, true);
    $this->load->view('template_admin_v', $template_data);
}

and after:

  1. edit upload.php
    • system -> libraries -> Upload.php
  2. function do_upload()
    • line 202
    • $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
  3. edit line 202 for:
    • $this->file_name = $this->_prep_filename(preg_replace('/[^(\x20-\x7F)]*/','', $_FILES[$field]['name']));

Upvotes: 2

ilbesculpi
ilbesculpi

Reputation: 328

You can always rename the file using something like:

$new_file_name = preg_replace('/[^(\x20-\x7F)]*/','', $_FILES['result']['name']);
$config['file_name'] = $new_file_name;

and then

if( $this->upload->do_upload() ) {
    // do your stuff
}

Upvotes: 0

Related Questions