Welcome Guest, Not a member yet? Register   Sign In
How do I store an uploaded file in the database?
#11

[eluser]gtech[/eluser]
I have recently needed to solve this problem as my client wants thier image data to be stored in a database. the key is to use addslashes() and removeslashes() when inserting and retrieving the data in a database.

I have posted up what I have done to get it working, to save others head scratching.. my database table contains a mediumblob to store images. In the upload function I store the contents of the uploaded file in a variable called $content and call addslashes($content); so it can be inserted into the database.

Code:
...
  $data = array('upload_data' => $this->upload->data());

  $fileName = $data['upload_data']['orig_name'];
  $tmpName  = $data['upload_data']['full_path'];
  $fileSize = $data['upload_data']['file_size'];
  $fileType = $data['upload_data']['file_type'];

  $fp      = fopen($tmpName, 'r');
  $content = fread($fp, filesize($tmpName));
  $content = addslashes($content);
  fclose($fp);

  // my model just does a simple insert.
  $ret = $this->contents_model->insert($fileSize, $fileType, $content, $objid);
  unlink($tmpName);
...

then to display it I use stripslashes()

Code:
<?php
class Content_show extends Controller {

  function Content_show()
  {
    parent::Controller();
  }
  function imageshow($content_id)
  {
    // this line is extracted from the model
    $q = $this->db->getWhere('content_items',array('ContentItemID'=>$content_id));

    $q = $q->row();
    echo header("Content-Type: $q->Type");
    $content=stripslashes($q->Data);
    echo $content;
  }
}
?>
#12

[eluser]Dan Tdr[/eluser]
hey gtech i would need some help please if you can help me out it would be just perfect..

ok so here`s my problem... i want to upload a file on my server and then put the file name in the database

i have the files from the userguide upload file.. i have a do_upload function and 2 files upload_form and upload_success.. now what i want is: after i click the upload button from the upload form the original file name to be stored into the database i saw you accessed it this way : $fileName = $data['upload_data']['orig_name']; now.. it would be hard for you to post all 3 files modifide to upload a file on my server and then ut the file name into the database? i mean.. could you please put all the things together? i can`t seem to figure it out.. and i`ve been trying for abut a week now.. and still not even close Sad

thanks,
Dan
#13

[eluser]Colin Williams[/eluser]
It just makes more sense to me to store binary data on the file system, especially in a Web development environment. If a client simply said "I want to store images in a database" I wouldn't take that to mean they literally want binary data stored in the database, although I would clarify just in case.
#14

[eluser]Unknown[/eluser]
Some people may also find the PHP function serialize useful for storing the file metadata in a database. That way you can store the whole $this->upload->data() array without throwing away information that may be useful in the future, eg: image sizes, file sizes etc.

http://us3.php.net/manual/en/function.serialize.php
#15

[eluser]Franklin2988[/eluser]
Hi everybody I was trying to save images in the database but I get this error
Code:
A Database Error Occurred

Error Number: 1300

Invalid utf8 character string: 'õ\0\0ððð\0\0\0ÛÛÛÆÆƱ±±¤¤¤™™™ÏÏÏ­­­‘‘‘×××ÌÌÌ   ———¦¦¦ÀÀÀèèèžžžÈÈ'

INSERT INTO `data` (`GIF89a\0\0õ\0\0ððð\0\0\0ÛÛÛÆÆƱ±±¤¤¤™™™ÏÏÏ­­­‘‘‘×××ÌÌÌ

I followed gtech example but when I get that error, If you can help me solving this It would be great.




Theme © iAndrew 2016 - Forum software by © MyBB