Active Record插入查询以在mysql数据库中保存图像

时间:2013-04-09 13:54:51

标签: codeigniter

您好我想将图像保存到数据库..我写了一些代码,但它工作不正常。我的控制器代码是

function UploadImageView()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);

        $imgdata['file']=$_FILES;
        $this->load->model('Users');
        /*$response=$this->Users->Image_upload();
        header('Content-type: application/json');
        echo json_encode($response);
   */

        foreach($_FILES as $key => $value)
        {

            $this->upload->initialize($config);


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

                        //var_dump($error);
                        $msg = $this->upload->display_errors('<p>', '</p>');
                        echo 'errore: '.$msg;


            }
            else
            {
               $this->load->model('Users');
               $this->Users->Image_upload($key);
            }
        }
    }

我的模型代码如下。

function Image_upload($value)
{
    $this->filedata=$value;
    $handle = fopen($this->filedata,"rb");
    $img =fread($handle, filesize('$filedata'));
    fclose($handle);
    $img = base64_encode($img);
    $data=array(
        'image'=>$img
    );
    $flag=$this->db->insert('testimage',$data);

    if($flag)
            {
                $this->db->where('image', $this->filedata);
                $query = $this->db->get('testimage');

                if ($query->num_rows() == 0)
                {
                    $response['status'] = false;
                    $response['userId'] = -1;           
                    $response['message'] = "Upload Failed!";
                }
                else
                {
                    $result = $query->result();
                    $response['status'] = true;
                    $response['file'] = $this->filedata;
                    $response['message'] = "Success!";      
                }   
            }
return $response;           
}

它返回错误 “fopen(Imgupload):无法打开流:没有这样的文件或目录”

和 消息:filesize():$ filedata失败 和 fread()期望参数1是资源,布尔给定等等 所以任何人都帮我保存我的数据库中的图像。

1 个答案:

答案 0 :(得分:1)

if ( ! $this->upload->do_upload())
{
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('upload_form', $error);
}
else
{
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('upload_success', $data);
}

直接来自docs

如果您注意到,他们正在将上传数据发送到视图。因此,请将其发送到模型:

$this->Users->Image_upload($this->upload->data());

然后您的数据在数组中可用,这也在文档中解释:

  

$这 - &GT; upload-&GT;数据()

     

这是一个辅助函数,它返回一个包含所有数组的数组   与您上传的文件相关的数据。这是数组原型:

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

所以你可以像这样访问模型中的数据:

function Image_upload($data)
{
    $handle = fopen($data['full_path'],'rb');
    ...
相关问题