2012-02-09 15 views
2

2つの画像をアップロードしてサイズを変更し、データベースに保存しようとしていますが、いくつか問題があります。私は以下のコントローラを投稿しました。 print_r()my $ db_info配列で、photo_oneとthumb_oneの値を取得していますが、photo_twoまたはthumb_twoの値は取得できません。私が$ _FILES配列をprint_r()すると、すべてがそこにあるはずです。私のコントローラーが2番目の画像を処理できなくなっているものが誰かに見えますか?感謝!CodeIgniterを使用して2つの画像をアップロード、サイズ変更、保存する

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Model extends CI_Controller { 

    protected $_page = 'model'; 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function index() { 
     $this->load->model('Model_model'); 
     $data['page'] = $this->Global_model->pageInformation($this->_page); 
     $data['testimonials'] = $this->Model_model->getTestimonials(); 
     $data['success'] = FALSE; 
     $this->load->helper('form'); 
     $this->load->library('form_validation'); 
     $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
     if ($this->input->post('submit')) { 
      $this->form_validation->set_rules('name', 'Name', 'trim|required'); 
      $this->form_validation->set_rules('stage_name', 'Stage Name', 'trim'); 
      $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email'); 
      $this->form_validation->set_rules('birth_month', 'Birth Month', 'trim|required|integer|exact_length[2]'); 
      $this->form_validation->set_rules('birth_day', 'Birth Day', 'trim|required|integer|exact_length[2]'); 
      $this->form_validation->set_rules('birth_year', 'Birth Year', 'trim|required|integer|exact_length[4]'); 
      $this->form_validation->set_rules('age_check', 'Age Check', 'trim|required'); 
      $this->form_validation->set_rules('ip', 'IP Adress', 'trim|required|valid_ip'); 
      if($this->form_validation->run() == FALSE) {} else { 
       if ($this->upload()) { 
        $email = $this->input->post('email'); 
        $body = "<p></p>" 
        $this->load->library('email'); 
        $this->email->from('', ''); 
        $this->email->to($email); 
        $this->email->cc(''); 
        $this->email->subject(''); 
        $this->email->message($body); 
        if ($this->email->send()) { 
         $data['success'] = TRUE; 
        } 
       } 
      } 
     } 
     $this->load->view('model_view', $data); 
    } 

    public function upload() { 
     // Load necessary model and libraries. 
     $this->load->model('Model_model'); 
     $this->load->library('upload'); 
     $this->load->library('image_lib'); 

     // Set configuration array for uploaded photo. 
     $config['upload_path'] = 'models/'; 
     $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
     $config['max_size'] = '2048'; 

     // Set configuration array for thumbnail. 
     $config_thumb['image_library'] = 'GD2'; 
     $config_thumb['create_thumb'] = TRUE; 
     $config_thumb['maintain_ratio'] = TRUE; 
     $config_thumb['width'] = 150; 
     $config_thumb['height'] = 150; 

     // Upload first photo and create a thumbnail of it. 
     if (!empty($_FILES['photo_one']['name'])) { 
      $this->upload->initialize($config); 
      if ($this->upload->do_upload('photo_one')) { 
       $photo_info = $this->upload->data(); 
       $config_thumb['source_image'] = $photo_info['full_path']; 
       $db_info['photo_one'] = $photo_info['file_name']; 
       $this->image_lib->initialize($config_thumb); 
       $this->image_lib->resize(); 
       $db_info['thumb_one'] = $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext']; 
      } else { 
       $data['upload_error'][] = $this->upload->display_errors(); 
      } 
     } 
     // Upload second photo and create a thumbnail of it. 
     if (!empty($_FILES['photo_two']['name'])) { 
      $this->upload->initialize($config); 
      if ($this->upload->do_upload('photo_two')) { 
       $photo_info = $this->upload->data(); 
       $config_thumb['source_image'] = $photo_info['full_path']; 
       $db_info['photo_two'] = $photo_info['file_name']; 
       $this->image_lib->initialize($config_thumb); 
       $this->image_lib->resize(); 
       $db_info['thumb_two'] = $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext']; 
      } else { 
       $data['upload_error'][] = $this->upload->display_errors(); 
      } 
     } 
     print_r($db_info); 
     // $this->Model_model->submit($db_info); 
    } 

}

+1

私は初心者の間違いを見落とす犠牲者でした。アップロードクラスと同じイメージをアップロードするために私のメソッドに名前を付けました。だから私がクラスを呼び出すと、物事が混ざり合っていた。レッスンで学び、問題を解決しました。 – WebDev84

+0

答えとして投稿し、これを正しいと確認すると、未回答の質問を見ている人はこの1つをスキップできます;-) –

答えて

1

私は初心者のミスを見下ろすの犠牲者でした。アップロードクラスと同じイメージをアップロードするために私のメソッドに名前を付けました。だから私がクラスを呼び出すと、物事が混ざり合っていた。レッスンで学び、問題を解決しました。

関連する問題