Welcome Guest, Not a member yet? Register   Sign In
File upload to database
#1

[eluser]junaids[/eluser]
hi.
there is a CI upload class that allows to upload files to a destination folder.
i want to upload images to a database table. how is that be done?
#3

[eluser]junaids[/eluser]
i followed this thred
http://ellislab.com/forums/viewthread/81832/

and got into an error. my code is like this
Code:
function do_upload()
    {
        $config['upload_path'] = './profile_imgs/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '1000';
        $config['max_width']  = '0';
        $config['overwrite']  = FALSE;
        $config['remove_spaces']  = TRUE;
        $field_name = "file_name";
        
        $this->load->library('upload', $config);
        
         if ( ! $this->upload->do_upload($field_name))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('profileimage', $error);
        }    
        else
        {
          $data = array('upload_data' => $this->upload->data());
          $tmpName  = $data['upload_data']['full_path'];
                 $fileSize = $data['upload_data']['file_size'];

             $fp      = fopen($tmpName, 'r');
                 $content = fread($fp, filesize($tmpName));
                 $content = addslashes($content);
         fclose($fp);
         unlink($tmpName);
         $content = stripslashes($content);
         $this->db->where('user_id', '2');
         $this->db->update('user', $content);
        }
    }

the error is
Error Number: 1300

Invalid utf8 character string: '‰PNG'

UPDATE `user` SET ‰PNG...and a large string of characters.
the file is uploaded to the specified path. but its not updated in the Db.
any idea about the error!
#4

[eluser]junaids[/eluser]
if anybody can mark my mistake or post some other way to do the job!
#5

[eluser]TheFuzzy0ne[/eluser]
The last line should probably be something like:

Code:
$this->db->update('user', array('content' => $content));

The array key specifies the name of the field under which to store the data. Hope this helps.
#6

[eluser]junaids[/eluser]
thanks. its working now.
#7

[eluser]junaids[/eluser]
now i get an error if i upload files larger then 1MB.
DB field is longblob.
also in upload config i have,
Code:
$config['max_size']    = '16777215';
i have checked php.ini for max upload size and it is 32 MB.
the error is,

Error Number: 1153

Got a packet bigger than 'max_allowed_packet' bytes
any idea?
#8

[eluser]Dam1an[/eluser]
There is a variable max_allowed_packets in /etc/my.cnf
Increase this and restart MySQL and it should solve it

It should fix the problem, but not sure if its advisable
#9

[eluser]junaids[/eluser]
sorry to again disturb u but i cant find the folder u mentioned /etc/my.cnf
#10

[eluser]Dam1an[/eluser]
Is this a production server, windows/UNIX, localhost on XAMPP?




Theme © iAndrew 2016 - Forum software by © MyBB