0

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

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(): stat failed for $filedata 和 fread() 期望参数 1 是资源、给定的布尔值等等等,所以任何人都可以帮助我将图像保存在我的数据库中。

4

1 回答 1

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);
}

直接来自文档

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

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

然后您的数据在数组中可用,如文档中所述:

$this->上传->数据()

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

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');
    ...
于 2013-04-09T14:25:20.143 回答