codeigniter image uploading mysql

大憨熊 提交于 2019-12-03 22:02:41

问题


I wanted to know if I was uploading an image in CodeIgniter to a database what would be my

$config['upload_path']

All the examples I have seen are using the filesystem. I have articles in a db and would like to store images relating to articles in the db as well. Can anyone help?


回答1:


You can read this great article called Storing Images in Mysql.

The article covers the following:

  • Isn’t this a bad idea?
  • What is a BLOB?
  • Creating an image table
  • The upload form
  • Uploading the image
  • The upload() function
  • Display an image from the database
  • Displaying all the information

But not to leave you empty handed, look into Blob, it's a data-type for colums in MySQL ( and various other dbms ). This will let you store data such as Images and other binary file-types.

The idea of storing files and images in the database is in general the same as storing them on the filesystem, the layer in-between upload and having the actual file is just different.

You cannot just set your upload-path and hope everything is solved, you need to get some dirt on your hands aswell!




回答2:


i posted here in hope this help someone since this is an old post and the answers are no help at all, this code works for me, also the model part is not here so you must figure it out reading codeigniter's docs , i think this will work if you put it in your controller, also it think the submit form must be pointing this function

function upload() {
 $caption = $this->input->post('caption'); 
    $codigo = $this->input->post('codigo');
    //$imagen = $this->input->post('imagen');
    $config['upload_path'] = 'uploads';// this is a directory with 777 permissions where you upload the file
    $config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
    //$config['max_size'] = '5000';
    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('imagen')) { // this is the input from the form
        echo $this->upload->display_errors();
    } else {
        //here $file_data receives an array that has all the info
        //pertaining to the upload, including 'file_name'
        $file_data = $this->upload->data();

        $fp = fopen($file_data['full_path'], 'r');
        $content = fread($fp, filesize($file_data['full_path']));
        //$content = addslashes($content);
        fclose($fp);
        $data = array( // this is the table i got in my db
            'idBotones' => null,
            'imagen' => $content, //blob image
            'caption' => $caption,
            'codigo' => $codigo
        );

        $this->load->model('generic_model');
        $table = "botones";
        $this->generic_model->insertar_datos($table, $data);
       //print_r($content);
    }
}



回答3:


It seems like for most common use cases storing images in the database is not a great idea. Please see these two previous SO threads:

To Do or Not to Do: Store Images in a Database

Storing Images in DB - Yea or Nay?



来源:https://stackoverflow.com/questions/1650275/codeigniter-image-uploading-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!