首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用codeigniter上传时,mysql中不提供镜像

使用codeigniter上传时,mysql中不提供镜像
EN

Stack Overflow用户
提问于 2018-07-25 02:24:45
回答 2查看 53关注 0票数 -1
  1. 问题:尝试通过codeigniter服务在mysql数据库字段(BLOB)中存储镜像时,MySql未启用该镜像。(我知道这不是一种好的做法,但却是必须的)。什么时候是上传镜像通过mysl管理,镜像它的ok。
  2. 我如何测试?我使用postman,它有几个文本字段和一个“文件”字段,我将图像保存在数据库中。

Postman

  1. 测试结果:所有文本字段都存储在数据库中,文件字段按预期上传到目录中,但二进制数据没有存储在数据库中的字段foto中

Results in MySql

Codeigneter:中的

  1. 代码

代码语言:javascript
复制
public function CrearCliente(){
    $user = $this->input->post('user');
    $nombre = $this->input->post('nombre');
    $apellido = $this->input->post('apellido');
    $rut = $this->input->post('rut'); 
    $correo = $this->input->post('correo');
    $id_pais = $this->input->post('id_pais');
    $fecha = $this->input->post('creacion');
    $password = $this->input->post('password');
    $presentacion = $this->input->post('presentacion'); 
    $telefono = $this->input->post('telefono'); 
    $long = 0;
    $lat = 0;
    $res="null";
		$this->load->library('form_validation');
		$mimetypes = array("image/jpeg", "image/pjpeg", "image/gif", "image/png");
		$foto = $_FILES['foto']['name'];
		$tamanio = $_FILES['foto']["size"];
		$archivo = $_FILES['foto']['tmp_name'];
		$type = $_FILES['foto']['type'];
		$imagen = fopen($archivo, 'rb');
    $contenido2 = fread($imagen, $tamanio);
  	$contenido=file_get_contents($archivo,TRUE);
		$contenido3 = $this->db->escape_str($contenido);
	  fclose($imagen);
		if(!in_array($type, $mimetypes))
    											die("El archivo que subiste no es una imagen válida");
                          
		$config['upload_path'] = './imagenes/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size'] = '5000000';
		$config['quality'] = '90%';
		$config['overwrite'] = TRUE;
		$config['max_width']            = 1024;
    $config['max_height']           = 768;
		$config['file_name'] = $foto;
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('foto')) {
								$error = array('error' => $this->upload->display_errors());
								$data['uploadError'] = $this->upload->display_errors();
								die('error');
                }
		else{
								$data = array('upload_data' => $this->upload->data());
										}
		
    $this->load->model('Usuario');
    $res = $this->Usuario->InsertUsuario($contenido3,$user, $nombre, $apellido,$rut, $correo, $foto,$tamanio,$id_pais,$fecha,$password,$presentacion,$telefono,$long,$lat);   //	*se le asigna una variable al modelo para guardar el contenido que retorna la bd
    header('content-type: application/json'); //* se colcoa una cabezera para decir que se va a retornar un Json
    echo json_encode($res);		
   }




function InsertUsuario($contenido,$user, $nombre, $apellido,$rut, $correo, $foto,$tamanio,$id_pais,$fecha,$password,$presentacion,$telefono,$long,$lat)
   {
   
     $hoy=getdate();   
     if ($foto) {
       echo $contenido;
       
      }
    else{
       $imagen = null;
       header('content-type: application/json');
       echo json_encode("./upload/error.jpg");
    }   
     $this->db->trans_strict(TRUE);
     $this->db->trans_start();   
     $data = array(
         'user'          =>   $user,
         'nombre'          =>   $nombre,
         'apellido'          =>   $apellido,
         'id_rut'          =>   $rut,
         'correo'          =>   $correo,
         'foto'          =>   $contenido,
         'id_pais'          =>   $id_pais,
         'password'          =>   $password,
         'presentacion'  =>    $presentacion,
         'telefono'  =>    $telefono,
         'creacion'          =>   $fecha,
         'long'          =>   $long,
         'lat'          =>   $lat
         );
  
         $this->db->insert('wm_clientes',$data);
         $this->db->trans_complete();
         if ($this->db->trans_status() === FALSE)
             {
               return $res='ERROR';
             }
             else
             {
               return $res='OK';
             }
   }

  1. 问题:为什么与图像(jpg)文件相关的数据不存储在数据库中?检查图像。我尝试了很多东西,但看起来比我预期的更难。需要帮助大师!strong text
EN

回答 2

Stack Overflow用户

发布于 2018-07-25 05:04:54

试试这个:

代码语言:javascript
复制
public function CrearCliente() {
    $user = $this->input->post('user');
    $nombre = $this->input->post('nombre');
    $apellido = $this->input->post('apellido');
    $rut = $this->input->post('rut');
    $correo = $this->input->post('correo');
    $id_pais = $this->input->post('id_pais');
    $fecha = $this->input->post('creacion');
    $password = $this->input->post('password');
    $presentacion = $this->input->post('presentacion');
    $telefono = $this->input->post('telefono');
    $long = 0;
    $lat = 0;
    $res = "null";
    $this->load->library('form_validation');
    $mimetypes = array("image/jpeg", "image/pjpeg", "image/gif", "image/png");
    $foto = $_FILES['foto']['name'];
    $tamanio = $_FILES['foto']["size"];
    $archivo = $_FILES['foto']['tmp_name'];
    $type = $_FILES['foto']['type'];
    //$imagen = fopen($archivo, 'rb');
    //$contenido2 = fread($imagen, $tamanio);
    //fclose($imagen);
    if (!in_array($type, $mimetypes)) {
        // ci upload checks mimes for you
        show_error("El archivo que subiste no es una imagen válida");
    }

    $config['upload_path'] = './imagenes/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '5000000';
    $config['quality'] = '90%';
    $config['overwrite'] = TRUE;
    $config['max_width'] = 1024;
    $config['max_height'] = 768;
    $config['file_name'] = $foto;
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload('foto')) {
        //$error = array('error' => $this->upload->display_errors());
        //$data['uploadError'] = $this->upload->display_errors();
        show_error($this->upload->display_errors());
    }
    $contenido = file_get_contents($this->upload->data('full_path'));
    $contenido3 = $this->db->escape_str($contenido);
    $this->load->model('Usuario');
    $res = $this->Usuario->InsertUsuario($contenido3, $user, $nombre, $apellido, $rut, $correo, $foto, $tamanio, $id_pais, $fecha, $password, $presentacion, $telefono, $long, $lat);   //  *se le asigna una variable al modelo para guardar el contenido que retorna la bd
    header('content-type: application/json'); //* se colcoa una cabezera para decir que se va a retornar un Json
    echo json_encode($res);
}

function InsertUsuario($contenido, $user, $nombre, $apellido, $rut, $correo, $foto, $tamanio, $id_pais, $fecha, $password, $presentacion, $telefono, $long, $lat) {

    $hoy = getdate();
    if ($foto) {
        echo $contenido;
    } else {
        $imagen = null;
        header('content-type: application/json');
        echo json_encode("./upload/error.jpg"); // this doesn't seem valid, also you should probably exit
    }
    // no need for transaction when only running 1 query
    $this->db->trans_strict(TRUE);
    $this->db->trans_start();
    $data = array(
        'user' => $user,
        'nombre' => $nombre,
        'apellido' => $apellido,
        'id_rut' => $rut,
        'correo' => $correo,
        'foto' => $contenido,
        'id_pais' => $id_pais,
        'password' => $password,
        'presentacion' => $presentacion,
        'telefono' => $telefono,
        'creacion' => $fecha,
        'long' => $long,
        'lat' => $lat
    );
    $this->db->insert('wm_clientes', $data);
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE) {
        return $res = 'ERROR';
    } else {
        return $res = 'OK';
    }
}

我还建议将数据数组发送到insert函数,而不是将每个数组作为参数-它是杂乱无章的,容易出错。我也不建议混合使用字符串返回类型和json返回类型,因为它是一个不一致的接口。另外,如果你正在使用ajax,你需要使用FormData或者一个插件来上传一个文件,以防你没有这样做。

票数 0
EN

Stack Overflow用户

发布于 2018-07-27 03:04:23

解决这个问题的任何简单方法都是将图像存储在您的服务器上(您已经使用代码这样做了),然后将图像名称作为varchar保存到数据库中。

代码语言:javascript
复制
$config['upload_path'] = './imagenes/';

上面是上传到你的服务器的代码。它使用codeigniter提供的上传库转到您的服务器上的该路径。

现在,您可以在数据库中查询图像名称,而不是实际的图像,并在视图中的图像标记中使用它。这将从服务器中拉出图像。

如果您对此方法有任何疑问,请与我联系。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51505294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档