2016-07-08 67 views
0

嗨,我想上傳只使用代碼igniter.I圖像文件只需上傳PNG,JPG格式,jpeg文件。但其他類型的文件也上傳,如.txt,.mp3等。我的代碼給出below.please幫助我馬上解決這個問題。感謝所有提前。如何僅使用代碼點火器上傳圖像文件?

$config['upload_path'] = './avatar/'; 
    $config['allowed_types'] = 'gif|jpeg|png|jpg'; 
    $config['max_size'] = '1024mb'; 
    $config['max_width'] = '3000'; 
    $config['max_height'] = '3000'; 
    $this->load->library('upload', $config); 

    if (!$this->upload->do_upload()) { 

     $ve['data'] = $this->upload->display_errors(); 
    } else { 
     $this->upload->data(); 
    } 

回答

0
$config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '100'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 

$this->load->library('upload', $config); 
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the //class: 
$this->upload->initialize($config); 

if($this->upload->do_upload()) 
{ 
    $data = array('upload_data' => $this->upload->data()); 
    $this->load->view('upload_success',$data); 
}else 
{ 
    $error = array('error' => $this->upload->display_errors()); 
    $this->load->view('file_view', $error); 
} 
0

我認爲你需要在用戶文件/上傳的文件派試圖如果條件中進行驗證。

if (! $this->upload->do_upload('userfile')) 
{ 
    $error = array('error' => $this->upload->display_errors()); 
    $this->load->view('upload_form', $error); 
} 
else 
{ 
    $this->upload->data(); 
}