2

ユーザーが送信するフォルダーに画像を保存しています。しかし、特殊文字 (ç、á、ã など) を含む画像を保存すると、システムが認識せず、ファイル名が間違って保存されます。

例: ファイル名:cação.pngおよび保存:caà § à £ o

コントローラ:

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);
}

モデル:

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);
}

ご協力いただきありがとうございます!

4

2 に答える 2

2

コントローラ:

    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);
}

以降:

  1. upload.php を編集
    • システム -> ライブラリ -> Upload.php
  2. 関数 do_upload()
    • 202行目
    • $this->file_name = $this->_prep_filename($_FILES[$field]['name']);
  3. 行 202 を次のように編集します。
    • $this->file_name = $this->_prep_filename(preg_replace('/[^(\x20-\x7F)]*/','', $_FILES[$field]['name']));
于 2013-06-04T16:52:57.490 に答える
0

次のようなものを使用して、いつでもファイルの名前を変更できます。

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

その後

if( $this->upload->do_upload() ) {
    // do your stuff
}
于 2013-06-04T15:19:43.407 に答える