2011-02-13 144 views
0

您好所有IM一個管理系統,可以製造新聞與圖片,但我不能找出如何從我的模型文件發送圖像名稱到我的控制器上工作,笨上傳圖片

這是我的模型文件:

function uploadImg() 
{ 
    $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 # 

    $file_array = $this->upload->data('file_name'); 
    return $billed_sti['billed_sti'] = $file_array['file_name']; 

    //$this->db->where('username', $this->input->post('username')); 
    //$this->db->update('users', $profilBilledNavn); 
} 

這是我的控制器:

function opret() { 

    $this->form_validation->set_rules('overskrift', 'overskrift', 'required'); 
    $this->form_validation->set_rules('description', 'description', 'required'); 
    $this->form_validation->set_rules('indhold', 'indhold', 'required'); 

    if($this->form_validation->run() == true) 
    { 

     $this->load->model('admin/nyheder_model'); 
     $billed_sti = $this->nyheder_model->uploadImg(); 

     $data = array(
      'overskrift' => $this->input->post('overskrift'), 
      'description' => $this->input->post('description'), 
      'indhold'  => $this->input->post('indhold'), 
      'billed_sti' => $billed_sti, 
      'brugernavn' => $this->session->userdata('username'), 
      'godkendt' => 'ja' 
     ); 

     $this->db->insert('nyheder', $data); 

     redirect('admin/nyheder/index'); 
    } else { 
     $this->index(); 
    } 


} 
+1

Alex已經提到過,你不應該把它放在你的模型中 - 它應該在你的控制器中。 – Repox 2011-02-14 13:14:08

回答

0

請使用上傳功能在你的控制器作爲模型類用於處理數據庫的信息。請檢查下面的代碼

//控制器類

function upload_image() 
{ 
//Check for the submit 
// Submit Name refers to the name attribute on the submit input tag. 
// $filename refers to the name attribute of the file input tag. 

if($_SERVER['REQUEST_METHOD'] == "POST") 
{ 
$submit = $this->input->post('submit'); 
if($submit == "Submit Name") 
{ 
//Load the relevant classes and libraries 
$this->load->library('upload'); 
$this->load->model('admin/nyheder_model','nmodel'); 
$filename = "image_file"; 
//Define the config array 
$config = array(); 
$config['upload_path'] = $this->gallery_path; 
$config['allowed_types'] = "jpg|gif|png"; 
$config['max_size'] = 0; //0 is for no limit 

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

if(!$this->upload->do_upload("$filename")) 
{ 
echo $this->upload->display_errors(); 
} 
else 
{ 
$file_data = $this->upload->data(); 
$filename_1 = $file_data['file_name']; 
$insert_array = array('filename'=>"$filename_1"); 
$this->nmodel->insert_data($insert_array); 
} // end of the else statement 
} // end of the isset statement 
} // end of the outer conditional statement 

現在你已經在$ filename_1變量的文件名,你可以傳遞給模型類,並可以存儲在數據庫中的值的值。

感謝 Ĵ

1

你需要做的是移動代碼的圖像上傳到CONTROLER。

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

一旦你這樣做,

您可以在此行中創建的$數據變量插入的文件的名稱:

$data = array('upload_data' => $this->upload->data()); 

,你可以得到這樣的價值:

$ data ['file_name']

該文件將上傳您配置的文件夾d,並且您將從控制器中將文件名插入數據庫。

我希望它有幫助。