开发者

uploading file to database

hi its my first time here. i really dont know what's wrong with my codes. iam trying to upload file to database yet when i click the upload开发者_运维百科 button, an error would occur.. (object not found) hope someone can help me...

btw, heres my code snippet

function do_upload()
{

    $config['upload_path'] = './uploads/';      
    $config['allowed_types'] = 'gif|jpg|png|doc|txt|pdf';
    $config['max_size'] = '5000';
    $config['max_width']  = '2500';
    $config['max_height']  = '2500';
    $config['remove_spaces']= 'true';

    $this->load->library('upload', $config);

    $data= array('userfile' => $this->upload->do_upload());

    //DEFINE POSTED FILE INTO VARIABLE
    $name= $data['userfile']['name'];
    $tmpname= $data['userfile']['tmpname'];
    $type= $data['userfile']['type'];
    $size= $data['userfile']['size'];

    //OPEN FILE AND EXTRACT DATA /CONTENT FROM IT
    $fp     = fopen($tmpname, 'r');
    $content= fread($fp, $size($tmpname));
    $content= addslashes($content);
    fclose($fp);


    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload', $error);
    }   
    else
    {
        $data = array('userfile' => $this->upload->data());
        $this->load->database();
        $ret=$this->db->insert($name, $type, $size, $content);
        unlink($tmpname);
        //$this->load->database();
        //$this->load->view('upload', $data);
    }
 }


The $this->db->insert() method does not work this way. It takes two arguments: the first one is the table into which you want to insert your data, the second is an array containing your data.

In your case, you should first put your file's data into an array :

$file_data=array('name'=>$name,'type'=>$type,'size'=>$size,'content'=>$content)

and then insert that into the appropriate table. I used files as an example. Use the one you actually need.

$ret=$this->db->insert('files',$file_data);

Please note though that except in some rare cases (file writing forbidden, etc...) it is generally a better idea to save files as ... files

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜