我有一个问题,如果用户没有选择图像,他可以按下“提交”底部,然后它将数据库中的名称编辑为空(因为没有图像),所以他的个人资料图片是空白的,我不想那样。我想显示用户没有选择图像的错误。Codeigniter图像上传
我的其他问题是当用户上传新图像时,我怎么能从服务器上删除旧的?
这里是我的代码我:
视图
<?php echo form_open_multipart('profil/uploadImg'); ?>
<div style="float:none; margin-bottom:5px;" id="profil_billed">
<img width="200" height="200" src="<?php echo base_url(); ?>images/users/<?php echo $query->profile_picture; ?>" alt="">
</div><!-- profil_billed -->
<?php echo form_upload('userfile'); ?><br><br>
<?php echo form_submit('upload', 'Upload billed', 'class=bottom'); ?>
<?php echo form_hidden('username', $this->session->userdata('username')); ?>
<?php echo form_close(); ?>
模型
function addProfileImg()
{
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000,
'encrypt_name' => true
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 200,
'height' => 200,
'encrypt_name' => true,
'max_size' => 2000
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
# Ret profil billed navn #
//$data = $this->upload->data('file_name');
//print_r($data);
$file_array = $this->upload->data('file_name');
$profilBilledNavn['profile_picture'] = $file_array['file_name'];
$this->db->where('username', $this->input->post('username'));
$this->db->update('users', $profilBilledNavn);
}
控制器
function uploadImg()
{
$this->form_validation->set_rules('upload', 'upload',
'required');
$this->form_validation->set_rules('userfile', 'userfile',
'required');
if($this->form_validation->run() != true)
{
$this->load->model('profil_model');
$this->profil_model->addProfileImg();
redirect('profil/indstillinger/'.$this->session->userdata('username'));
}
}
@Ross谢谢,但我得到这个错误:S致命错误:调用一个成员函数do_upload()一个非对象在 – ole 2011-03-16 20:16:36
好,我dident加载上传LIB :p所以现在它的工作原理我认为 – ole 2011-03-16 20:24:21
@Ross我现在尝试这个http://pastebin.com/LT3r60QC但它总是回显“错误”,如果我没有上传,如果我上传图像:S你可以看到什么错误? – ole 2011-03-16 20:57:55