2013-09-24 40 views
0

我使用CI tank_auth庫允許用戶一次編輯他們的配置文件,現在我被困在圖片(頭像)上傳中,我希望用戶上傳他們的圖片並存儲文件名在表中,我試圖獲取表中的文件名和更新,但沒有成功,具體領域留空。Codeigniter無法上傳並無法獲取file_name

OK,我試圖檢查控制器是否有hold選定的文件,我用flashdata檢查什麼是響應後上傳的,我所看到的是filename:,並預計在$image_data['file_name']不顯示,即使該文件是沒有上傳到特定路徑。

我不知道我的代碼有什麼問題,我花了幾個小時的解決方案,但沒有得到任何線索。

控制器:

public function index() 
{ 
    if(!$this->tank_auth->is_logged_in()) { 
     redirect('/auth/login'); 

    }else{ 

     $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|min_length[5]|max_length[30]'); 
     $this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean'); 
     $this->form_validation->set_rules('website', 'Website', 'trim|xss_clean|max_length[30]'); 

        // Here I do upload file check 

     if (!empty($_FILES['photo']['name'])) { 

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

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

      $image_data = $this->upload->data('photo'); 

      $this->session->set_flashdata('see', 'filename: '.$image_data['file_name']); 

     }else{ 

      $this->session->set_flashdata('see', 'where is my file?'); 
     } 


     $data['errors'] = array(); 

     if($this->form_validation->run()) { // validation ok 

      if(!is_null($data = $this->tank_auth->update_user(
       $this->form_validation->set_value('name'), 
       $this->form_validation->set_value('country'), 
       $this->form_validation->set_value('website'), 
       $this->form_validation->set_value($image_data['file_name']) 
      ))) { // success 

       $this->session->set_flashdata('msg', 'Profile has been updated'); 
       redirect(current_url()); 
      } 

     }else{ 

      $errors = $this->tank_auth->get_error_message(); 
      foreach($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v); 
     } 

     $user_id = $this->session->userdata('user_id'); 
     $profile = $this->users->get_profile_by_id($user_id); 

     $data = array(
      'uname' => $profile->name, 
      'ucountry' => $profile->country, 
      'uwebsite' => $profile->website, 
      'uavatar' => $profile->photo 
     ); 
    } 

    $data['title'] = $this->session->userdata('name').'\'s Account'; 

    $this->load->view('header', $data); 
    $this->load->view('my_account', $data); 
    $this->load->view('footer', $data); 
} 

查看:

<?php 
$photo = array(
    'name' => 'photo', 
    'id' => 'photo', 
    'size' => 30 
); 

if(($this->session->flashdata('see'))){ 
    echo ' 
     <div class="alert alert-info" style="margin:5px 0;"> 
      <button class="close" data-dismiss="alert" type="button">×</button> 
      '.$this->session->flashdata('see').' 
     </div>'; 
} 
?> 

<?php echo form_open_multipart($this->uri->uri_string()); ?> 
<table border="0"> 
    . 
    . // blah blah info fields here 
    . 
    <tr> 
    <td> 
    <div><?php echo form_label('Photo', $photo['id']); ?></div> 
    <div><?php echo form_upload($photo); ?></div> 
    <div class="field_error"><?php echo form_error($photo['name']); ?><?php echo isset($errors[$photo['name']])?$errors[$photo['name']]:''; ?></div> 
    </td> 
    </tr> 
</table> 

<div style="padding:20px 0;"><?php echo form_submit('submit', 'Save'); ?></div> 

<?php echo form_close(); ?> 
</div> 

請指教。謝謝。

回答

2

你錯過下面的行

$this->upload->do_upload("image") 

之前添加上面的代碼,

$image_data = $this->upload->data('photo'); 

希望這有助於你出去。

0

如果你想寫上傳的文件與CI服務器,那麼試試這個

$path_to_file = $config['upload_path'].$image_data['file_name']; 
write_file($config['upload_path'].$image_data['file_name'], .$image_data['file_name']); 

此外,請確保您加載文件幫助過如下

$this->load->helper('file');