【问题标题】:Codeigniter upload file and save name with special caractersCodeigniter 上传文件并用特殊字符保存名称
【发布时间】:2013-06-04 15:08:19
【问题描述】:

我将图像保存在要由用户发送的文件夹中。但是如果我用特殊字符(ç,á,ã等)保存图像,系统无法识别,并保存文件名错误。

示例:文件名: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);
}

感谢您的帮助!

【问题讨论】:

    标签: codeigniter file-upload special-characters


    【解决方案1】:

    控制器:

        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-&gt;file_name = $this-&gt;_prep_filename($_FILES[$field]['name']);
    3. 编辑第 202 行:
      • $this-&gt;file_name = $this-&gt;_prep_filename(preg_replace('/[^(\x20-\x7F)]*/','', $_FILES[$field]['name']));

    【讨论】:

      【解决方案2】:

      您始终可以使用以下方式重命名文件:

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

      然后

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

      【讨论】:

      • 不错。在数据库中保存井名文件,但在文件夹中不保存特殊字符
      猜你喜欢
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多