2017-02-28 90 views
0

我有一個包含不同輸入和文件上傳輸入的表單。我想要用戶,如果他們想上傳圖片然後上傳,但如果他們不想上傳。不要給出錯誤。Codeigniter上傳圖片選擇

if($_POST){   
    $config['upload_path']   = 'images/tmp'; 
    $config['allowed_types']  = 'gif|jpg|png'; 
    $config['max_size']    = 2048; 
    $config['min_width']   = 480; 
    $config['min_height']   = 360; 

    $this->load->library('upload', $config); 

    if (!$this->upload->do_upload('userfile')) { 
     $error = array('error' => $this->upload->display_errors()); 
     //Sending this $error to view and if a user didnt upload image and just filled 
     //the other inputs it shows error. but i dont want that. 
    } else { } 
} else { 
    redirect(site_url()); 
} 

回答

0

您正在正確的軌道上。只是刪除或註釋此行

$error = array('error' => $this->upload->display_errors());

你可能會送你$錯誤,查看文件,如下圖所示:

$this->load->view('upload_form', $error);

所以不要把你的價值來查看文件。只需撥打您的視圖時,圖像下面成功上傳:

 

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

       // $error = array('error' => $this->upload->display_errors()); 
       /*Delete or comment this line. Do your other stuff here if image not uploaded.*/ 

      }else{ 
        $data = array('upload_data' => $this->upload->data()); 
        $this->load->view('upload_success', $data); 
        /* This is example. You can do anything on successfully image upload.*/ 
      } 

你可以參考笨手動https://www.codeigniter.com/userguide3/libraries/file_uploading.html

+0

是的它沒關係,但其他錯誤如允許的類型如何。我想要顯示的只是圖像沒有上傳錯誤。 – codeigniter

0
$config['upload_path']   = 'images/tmp'; 
    $config['allowed_types']  = 'gif|jpg|png'; 
    $config['max_size']    = 2048; 
    $config['min_width']   = 480; 
    $config['min_height']   = 360;  
    $this->load->library('upload', $config); 
     //$rand_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
     // $filename=md5(str_shuffle($rand_str.mt_rand())); 
     // $config['file_name']=$filename; 

      $this->upload->initialize($config); 

      if ($this->upload->do_upload('userfile')) 
      { 
        //$data = array('upload_data' => $this->upload->data()); 
        // $data["filename"]=$filename; 
        // $data["ad_id"]=$lid; 

        // $data["filename"]=$rand_name; 
        // $this->Ads_model->insert_image($data); 



      } 
0

我解決我的問題改變上傳類給任何錯誤。我評論了包括「沒有選定的文件」的行。