2011-03-16 259 views
0

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

回答

0

CI的文件上傳有一些基本的錯誤處理 - from the docs

if (! $this->upload->do_upload()) // here 
    { 
     $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); 
    } 

我可以想象,如果該文件是空的,就會報錯。

至於刪除圖像,我想你必須存儲在數據庫中的圖像的路徑:

您可以使用unlink()

$path = '/path/to/my/img/upload/001.jpg'; // wherever you store this value. 

unlink($path); // returns true/false on success 
+0

@Ross謝謝,但我得到這個錯誤:S致命錯誤:調用一個成員函數do_upload()一個非對象在 – ole 2011-03-16 20:16:36

+0

好,我dident加載上傳LIB :p所以現在它的工作原理我認爲 – ole 2011-03-16 20:24:21

+0

@Ross我現在嘗試這個http://pastebin.com/LT3r60QC但它總是回顯「錯誤」,如果我沒有上傳,如果我上傳圖像:S你可以看到什麼錯誤? – ole 2011-03-16 20:57:55

0
 if (! $this->upload->do_upload()) 
     { 
      $error = array('error' => $this->upload->display_errors()); 
         // error ..show it to user 

     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 
         // save to db 

     } 

和刪除 你必須使用unlink() 然後你可能也必須從數據庫中刪除它。 你可以使用一個簡單的getwhere .. 例如:

$config['query'] = $this->db->get_where('images','some id here may be'); 
0

你應該做它用JavaScript

function validate() {

var extensions = new
Array("jpg","jpeg","gif","png","bmp");

// Alternative way to create the array
var extensions = new Array(){
extensions[1] = "jpg"; extensions[0] = "jpeg"; extensions[2] = "gif";
extensions[3] = "png"; extensions[4] = "bmp"; var image_file = document.form.image_file.value;
var image_length = document.form.image_file.value.length;
var pos = image_file.lastIndexOf('.')
+ 1; var ext = image_file.substring(pos, image_length);
var final_ext = ext.toLowerCase();

for (i = 0; i < extensions.length; i++)
{
if(extensions[i] == final_ext) {
return true;
}
}
alert("You must upload an image file with one of the following extensions: "+ extensions.join(', ') +"."); return false;
}

這種方式,你不會需要做服務器的請求。

**這個代碼是從http://www.bitrepository.com/validating-an-image-upload.html

+0

謝謝但如果用戶禁用javascript:/ – ole 2011-03-18 14:37:22