2017-05-05 161 views
0

我想創建一個使用CodeIgniter的圖像上傳編碼並將其保存在我的目標位置。這種編碼在localhost中工作正常,但是當我試圖將這種編碼上傳到Cpanel時,它給我一個錯誤。codeigniter上傳圖像(錯誤)

控制器代碼:

public function uploadPaid(){ 

    if ($this->input->post()) {      

     $arr = $this->input->post();     
     $this->load->database(); 
     $this->load->model('m_picture'); 
     $this->load->model('m_purchase'); 
     //$this->load->library('my_func'); 
     //$this->load->helper('url'); 
     $this->load->library('upload');      


     $config = array(
       'upload_path' => "./dist/invoice/", 
       'allowed_types' => "gif|jpg|png", 
       'overwrite' => TRUE, 
       'max_size' => "2000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
       'max_height' => "0", 
       'max_width' => "0", 
       'encrypt_name' => true 
      ); 

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

     $pur_id = $this->input->post('pur_id'); 
     $img_background = $this->input->post('fileImg'); 

     if ($this->upload->do_upload('fileImg')) 
     { 
      $data = $this->upload->data(); 
      $background="dist/invoice/".$data['raw_name'].$data['file_ext']; 
      echo $background; 
      $arr2 = array(         
        "img_url" => $background, 
        "ne_id" => $arr['pur_id']         
       );   

      $this->m_purchase->updateInv(1, $pur_id); 
      $this->m_picture->insert($arr2); 
      $this->session->set_flashdata('success' , '<b>Well done!</b> You successfully send the picture.'); 
      redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh'); 
     } 
     else 
     { 
      echo $this->upload->display_errors(); 
      $this->session->set_flashdata('warning' , '<b>Error!</b> You failed to send the picture.'); 
      redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh'); 
     } 
    }else{ 
     $this->session->set_flashdata('warning' , '<b>Uh Crap!</b> You got Error. The image size is to big'); 
     redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh'); 
    } 
} 

視圖代碼:

<form action="<?= site_url('purchase_v1/dashboard/uploadPaid'); ?>" method="POST" role="form" enctype="multipart/form-data"> 
    <div class="portlet-body flip-scroll" align="center"> 
     <span style = "color : #b706d6;"><h2><strong>#<?= (110000+$pur_id); ?></strong></h2></span> 
     <div class="form-group"> 
      <div class="fileinput fileinput-new" align="center" data-provides="fileinput"> 
       <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px; line-height: 150px;"></div> 
       <div> 
        <span class="btn btn-outline btn-file" style="background-color: #FF5733"> 
         <span class="fileinput-new"> Select image </span> 
         <span class="fileinput-exists"> Change </span> 
         <input type="hidden" value="" name="title"><input type="file" name="fileImg"> 
        </span> 
        <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> Remove </a> 
       </div> 

       <div class="clearfix">&nbsp;</div> 
       <button type="submit" class="btn btn-primary"><i class="fa fa-upload"> Submit</i></button> 
      </div> 
     </div> 
    </div> 
    <input type="hidden" name="pur_id" id="pur_id" class="form-control" value="<?= $pur_id; ?>"> 
</form> 
+0

請閱讀此:http://stackoverflow.com/a/41132834/2275490 – Vickel

+0

你可以在這裏粘貼錯誤? –

+0

實際上我上傳圖片後,據說這個編碼應該重定向到'purchase_v1/dashboard/page/a29',無論上傳圖片是否成功。但它只顯示一個空白頁面,所以我不知道它是什麼樣的錯誤。如果($ this-> upload-> do_upload('fileImg')) –

回答

0

你必須上傳庫的錯誤初始化。像這樣使用它。如果這不能解決問題,那麼上傳可能不是問題。這可能是代碼中其他地方的問題。代碼本身很差。

if ($this->input->post()) { 

     $arr = $this->input->post();     
     $this->load->database(); 
     $this->load->model('m_picture'); 
     $this->load->model('m_purchase'); 
     //$this->load->library('my_func'); 
     //$this->load->helper('url'); 
     $this->load->library('upload'); 

     $config = array(
     'upload_path' => "./dist/invoice/", 
     'allowed_types' => "gif|jpg|png", 
     'overwrite' => TRUE, 
     'max_size' => "2000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
     'max_height' => "0", 
     'max_width' => "0", 
     'encrypt_name' => true 
     ); 

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

     $pur_id = $this->input->post('pur_id'); 
     $img_background = $this->input->post('fileImg'); 


     if ($this->upload->do_upload('fileImg')) 
     { 
      $data = $this->upload->data(); 
      $background="dist/invoice/".$data['raw_name'].$data['file_ext']; 
      echo $background; 
     $arr2 = array(
        "img_url" => $background, 
        "ne_id" => $arr['pur_id'] 
       );   

     $this->m_purchase->updateInv(1, $pur_id); 
     $this->m_picture->insert($arr2); 
     $this->session->set_flashdata('success' , '<b>Well done!</b> You successfully send the picture.'); 
     redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh'); 
     } 
     else 
     { 
     echo $this->upload->display_errors(); 
     $this->session->set_flashdata('warning' , '<b>Error!</b> You failed to send the picture.'); 
     redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh'); 
     } 

    }else 
    { 
     $this->session->set_flashdata('warning' , '<b>Uh Crap!</b> You got Error. The image size is to big'); 
     redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh'); 
    } 

}