您好,我想将图像保存到数据库中。我编写了一些代码,但无法正常工作。我的控制器代码是
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 是资源、给定的布尔值等等等,所以任何人都可以帮助我将图像保存在我的数据库中。