简体   繁体   中英

Uploading two images, re-sizing, and saving to database with CodeIgniter

I am trying to upload two images, re-size them, and then save them in my database but am having some problems. I have posted my controller below. When I print_r() my $db_info array I am getting values for photo_one and thumb_one, but nothing for photo_two or thumb_two. When I print_r() the $_FILES array everything is in there like it should be. Can anyone see something I am missing that is preventing my controller from processing the second image? Thank!

<?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);
    }

}

I was a victim of overlooking beginner mistakes. I named my method for uploading images the same as the upload class. Therefore when I called for the class, things were getting mixed up. Lesson learned and problem solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM