我有一個問題,如果用戶沒有選擇圖像,他可以按下「提交」底部,然後它將數據庫中的名稱編輯爲空(因爲沒有圖像),所以他的個人資料圖片是空白的,我不想那樣。我想顯示用戶沒有選擇圖像的錯誤。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