Codeigniter映像更新 [英] codeigniter image update
本文介绍了Codeigniter映像更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在图片上传器上遇到了问题。我创建了可以正常工作的图片上传器,但我还需要对其进行编辑。
当我添加一个需要的图像时,数据库列会正确更新,但是如果不更改图像并保持原样,则会出现错误列'图像'不能为空
I'm stuck on a problem with image uploader. I've created the image uploader which works fine but I also need to edit them. When I add a need image the db column updates correctly but I if don't change the image and leave it as it is, I get an error "Column 'image' cannot be null"
这是更新部分的代码:
else if ($type == 'update')
{
if($this->input->post('image')) {
$fdata = $this->savenew();
$data['image'] = $fdata['upload_data']['file_name'];
} else {
$data['image'] = $this->input->post('current_image');
}
$return = $this->add_radio_model->update($id, $data);
}
对不起,如果我不太明确
Sorry if I wasn't very explicit
编辑:
private function savenew(){
$config['upload_path'] = './assets/'; //Make SURE that you chmod this directory to 777!
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0'; // 0 = no limit on file size (this also depends on your PHP configuration)
$config['remove_spaces']=TRUE; //Remove spaces from the file name
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image'))
{
$data['error']= array('error' => $this->upload->display_errors());
log_message('error',$data['error']);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
return $data;
}
推荐答案
根据我们的评论主题,在这里您需要更改代码
As per our comments thread, here you need to change your code
else if ($type == 'update')
{
if($this->input->post('image')) {
$fdata = $this->savenew();
// first always set current image
$data['image'] = $this->input->post('current_image');
// second check if new image added if yes, then update otherwise keep original image as it is
if(isset($fdata['upload_data'])){
$data['image'] = $fdata['upload_data']['file_name'];
}
}
$return = $this->add_radio_model->update($id, $data);
}
第二个问题:遇到PHP错误严重性:通知消息:数组到字符串的转换文件名:libraries / Log.php行号:99
更改:
$data['error']= array('error' => $this->upload->display_errors());
log_message('error',$data['error']);
TO
log_message('error',(string) $this->upload->display_errors());
这篇关于Codeigniter映像更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文