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

[eluser]hawthornpublications[/eluser]
Does anyone have tips on how to store a file in a database using CI? I found a basic tutorial on how to do with with straight PHP but want to know if there is a simple way to do it using CI.
#2

[eluser]trice22[/eluser]
I don't really know what you trying to achieve, but you usually store a reference to a file but not the file itself in a database.
Let's say you're uploading an image: You'll save it into a folder, maybe change the file-name and do some other modifications to it (like changing the size, cropping or rotating etc.) and then you just save the new/current file-name with or rather without the path to your image-folder in the DB.

Hope that gives you an better idea what your searching for?

—trice
#3

[eluser]Vince Stross[/eluser]
I'm interested to know this myself. I normally do not store my images in the database but there are times when this is a beneficial solution.

Hopefully the next person to reply can reply to the question and not question whether or not it should be done this way.

any takers?
#4

[eluser]wiredesignz[/eluser]
There is no special CI way of doing this. Perhaps you could port one of the thousand online examples to a library for us.
#5

[eluser]charlie spider[/eluser]
Just upload the image to a folder then store the renamed file in the database.

This is my function for a bakery site where they could upload pictures of wedding cakes to a gallery. The function will resize your images so that the longest length of the image is equal to whaterver you specify for the main image as well as the thumbnail size. Although i don't do it here, I would recommend adding a timestamp to the end of the renamed file and possibly hashing the filename altogether if you are paranoid about security. This was from my first CI project so it's not as ignited as it could be. Plus I have only been coding for about a year now part-time so I'm sure there are tons of inefficiencies in here. If anyone wants to comment I would greatly appreciate the input.

Code:
function upload()
{

$this->load->helper('url');
        
if($this->session->userdata('logged_in'))
{
    //User is logged in

    $this->load->model('CMS_model');    
    
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg';
    $config['overwrite'] = 'TRUE';
    $this->load->library('upload', $config);
                        
    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->cake_error($error);
    }    
    else
    {
        $data = $this->upload->data();
        foreach( $data as $item => $value ) { $filedata[$item] = $value; }
                
        $next = $this->Bakery_model->next_cake_ID();
        // instead of using auto_increment    
        $next = intval($next) + 1;
                
        $newfile = 'cake' . '_' . $next;
        rename($filedata['file_path'].$filedata['file_name'], $filedata['file_path'] . $newfile . '.jpg' );
        $path_n_file = $filedata['file_path'] . $newfile . '.jpg';
                
        $src_img = imagecreatefromjpeg($path_n_file);
        $src_size = getimagesize("$path_n_file");

        //set longest length for images in pixels
                
        $thumb_size = 72;
        $pic_size = 420;
                
        $src_size[0] = $filedata['image_width'];
        $src_size[1] = $filedata['image_height'];
            
        //generate thumb
                
        if ( $src_size[0] > $src_size[1] ) { $thumb_divisor = $src_size[0] / $thumb_size; }
        else { $thumb_divisor = $src_size[1] / $thumb_size; }            
                
        $thumb_width = $src_size[0] / $thumb_divisor;
        $thumb_height = $src_size[1] / $thumb_divisor;
                
        $thumb_img = imagecreatetruecolor($thumb_width, $thumb_height);
            
        imagecopyresampled($thumb_img, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $src_size[0], $src_size[1]);

        imagejpeg($thumb_img, $filedata['file_path'] . $newfile . '_thumb.jpg', 100);
        imagedestroy($thumb_img);
                
                        
        // generate main pic
                
        if ( $src_size[0] > $src_size[1] ) { $pic_divisor = $src_size[0] / $pic_size; }
        else { $pic_divisor = $src_size[1] / $pic_size; }            
                
        $pic_width = $src_size[0] / $pic_divisor;
        $pic_height = $src_size[1] / $pic_divisor;
        
        $pic_img = imagecreatetruecolor($pic_width, $pic_height);
            
        imagecopyresampled($pic_img, $src_img, 0, 0, 0, 0, $pic_width, $pic_height, $src_size[0], $src_size[1]);

        imagejpeg($pic_img, $filedata['file_path'] . $newfile . '.jpg', 100);
        imagedestroy($src_img);
        imagedestroy($pic_img);
                
        $this->Bakery_model->insert_cake($next, $newfile);
                
        redirect('weddingcakes/cake_cms', 'refresh');
    }
            
}
else
{
    //User is not logged in
    redirect('cms/login', 'refresh');            
}

}

The model stuff is super basic, although I use the active record method now cuz it's so much easier:

Code:
function insert_cake($ID, $cake_filename)
{
     $data = array( 'cake_ID' => $ID, 'cake_filename' => $cake_filename );
    $this->db->insert('tb_cake', $data);
}
    
function next_cake_ID()
{
    $sql = "SELECT cake_ID FROM tb_cake";
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0)
    {
          $row = $query->last_row();
        return $row->cake_ID;
    }
}
#6

[eluser]nirbhab[/eluser]
If you heading towards, a text of readable file contents, which can be stored in text format.
Answer is yes!
1. upload the file
2. save the location in db
3. read the content of uploaded file
4. save its content in the DB(LONGTEXT or TEXT, whatever you feel optimum for your application).
#7

[eluser]hawthornpublications[/eluser]
Great information, thank you to all.
#8

[eluser]nmweb[/eluser]
If you want to store in a mysql use BLOB field type in case you store binary files.
#9

[eluser]Unknown[/eluser]
[quote author="kridolfo" date="1206150819"]Does anyone have tips on how to store a file in a database using CI? I found a basic tutorial on how to do with with straight PHP but want to know if there is a simple way to do it using CI.[/quote]
#10

[eluser]hawthornpublications[/eluser]
minhajuddin, did you have a question?




Theme © iAndrew 2016 - Forum software by © MyBB