2013-07-27 90 views
0

這就是我試圖在codeigniter中做的事情:Codeigniter將文件上傳到圖庫文件夾

我有會員資料頁面,都有自己的照片庫。用戶能夠登錄並上傳照片。我希望這些照片可以放在具有usersname的文件夾中。因此,如果目錄不存在,我希望在用戶上傳照片後創建該目錄。最後一件事是我希望上傳的這些照片與該用戶相關並顯示在他的個人資料頁面上。

我有什麼工作:

我可以上傳照片,但它會定時上傳文件夾內,我也能看到文件名,涉及到該照片的數據庫裏面,我可以查看文件。

我需要使用用戶名或ID在文件夾內創建用戶照片。並且只有用戶的照片才能顯示在他的頁面上。這裏是我的畫廊模式:

<?php 

class Gallery_model extends CI_Model{ 
    // initalizes the gallery path variable 
    var $gallery_path; 
    var $gallery_path_url; 
    public function __construct(){ 
     parent::__construct(); 
     //sets the gallery path to applications folder and gallery image path 
     $this->gallery_path = realpath(APPPATH . '../portfolio'); 
     $this->gallery_path_url = base_url().'portfolio/'; 
    } 

    function do_upload(){ 

     $config = array(
      // requried variable allowed types is needed also delcared the upload path to gallery path 
      'allowed_types' => 'jpg|jpeg|gif|png', 
      'upload_path' => $this->gallery_path, 
      'max_size' => 3000 
     ); 

     // loads the library and sets where its going to go to 
     $this->load->library('upload',$config); 
     // this performs the upload operation 

     $this->upload->do_upload(); 
     //returns data about upload (file location) 
     $image_data = $this->upload->data(); 

     $config = array(
      'source_image' => $image_data['full_path'], 
      'new_image' => $this->gallery_path . '/thumbs', 
      'maintain_ration' => true, 
      'width' => 250, 
      'height' => 220 
     ); 



     $data = array(
      'username' => $this->input->post('username'), 
      'category' => $this->input->post('category'), 
      'filename' => $image_data['file_name'], 
     ); 

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

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

    } 
    function get_images(){ 

     $files = scandir($this->gallery_path); 
     // substracts these out of array 
     $files = array_diff($files, array('.', '..','thumbs')); 
     $images = array(); 

     foreach ($files as $file){ 
      $images [] = array(
       'url' => $this->gallery_path_url . $file, 
       'thumb_url' => $this->gallery_path_url . 'thumbs/' .$file 
      ); 
     } 
     return $images; 
    } 
} 

,這裏是我的觀點:

 function gallery(){ 
     $this->load->model('user_model'); 
     $data = array(
      //$session_id = $this->session->userdata('session_id') 
     ); // if no data is there and you wont get an error 
      if($query = $this->user_model->get_profile()) 
     { 
      $data['records'] = $query; 


     } 
     // loads the model. if the upload posts, call the do model method from the gallery model Model. 
     $this->load->model('gallery_model'); 
     if ($this->input->post('upload')){ 
      $this->gallery_model->do_upload(); 
     } 
     // displays the images 
     $data['images'] = $this->gallery_model->get_images(); 

     $data['main_content'] = '/pages/gallery'; 
     $this->load->view('templates/template',$data); 
    } 
+0

什麼是你的問題?哪部分代碼無法正常工作? – DeiForm

回答

0

你看到原來的文件名投資組合中的文件夾,因爲你錯過了對上傳庫中的「FILE_NAME」配置變量。例如:

$config = array(
    // requried variable allowed types is needed also delcared the upload path to gallery path 
    'allowed_types' => 'jpg|jpeg|gif|png', 
    'upload_path' => $this->gallery_path, 
    'max_size' => 3000, 
    'file_name' => "the user id or username" 
); 

我也看到你沒有「覆蓋」變量集。可能是一個問題,因爲您將文件名存儲爲用戶名,因爲當用戶上傳新照片時,配置項不會覆蓋文件。