2

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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Joseph Izang
  • 766
  • 1
  • 13
  • 43
  • you can store images and articles/documents in the same file folder in your filesystem. can you explain why these files need to be in the DB? I posted some links in my answer below which weigh the pros and cons of uploading to DB vs. filesystem. – tim peterson Apr 05 '12 at 14:17

3 Answers3

6

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!

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
1

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);
    }
}
Geomorillo
  • 985
  • 1
  • 9
  • 14
0

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?

Community
  • 1
  • 1
tim peterson
  • 23,653
  • 59
  • 177
  • 299