2013-07-27 4 views
0

Это то, что им пытаются сделать в CodeIgniter:Codeigniter Загрузка файлов в папку галереи

У меня есть страницы профиля пользователя, которые все имеют свою собственную галерею фотографий. Пользователь должен иметь возможность входа в систему и загрузки фотографий. Я хочу, чтобы фотографии были помещены в папку с именем пользователя. Поэтому, если каталог не exsit для пользователя, я хочу, чтобы он был создан после того, как он загрузит фотографию. Последнее, что я хочу, чтобы эти фотографии были загружены для связи с этим пользователем и отображались на его странице профиля.

Что я имею работу:

я могу загрузить фотографии, но она идет внутри папки обычной загрузки, я могу также видеть имя файла внутри базы данных, которая относится к этой фотографии, и я могу просматривать файлы.

Мне нужны фотографии пользователей, которые должны быть созданы внутри папки с именем пользователя или идентификатором. и только фотографии пользователя, которые будут отображаться на его странице. вот моя галерея модель:

<?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

Вы видите исходное имя файла в папке портфеля, потому что вы пропустили на «имя_файла» переменной конфигурации для библиотеки загрузки. Пример:

$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" 
); 

Я также вижу, что у вас нет «переписываемой» переменной. Может быть проблемой, потому что вы сохраняете имя файла как имя пользователя, так как CI не будет перезаписывать файл, когда пользователь загружает новую фотографию.

Смежные вопросы