【问题标题】:Uploading two images, re-sizing, and saving to database with CodeIgniter使用 CodeIgniter 上传两张图片、调整大小并保存到数据库
【发布时间】:2012-02-09 16:35:07
【问题描述】:

我正在尝试上传两张图片,重新调整它们的大小,然后将它们保存在我的数据库中,但遇到了一些问题。我在下面发布了我的控制器。当我 print_r() 我的 $db_info 数组时,我得到了 photo_one 和 thumb_one 的值,但没有得到 photo_two 或 thumb_two 的值。当我 print_r() 时,$_FILES 数组中的所有内容都应该在其中。谁能看到我缺少的东西阻止我的控制器处理第二张图像?谢谢!

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

}

【问题讨论】:

  • 我是忽视初学者错误的受害者。我将上传图片的方法命名为与上传类相同。因此,当我打电话去上课时,事情变得混乱了。吸取教训并解决问题。
  • 发布为答案并确认这是正确的,所以看到未回答问题的人可以跳过这个;-)

标签: php codeigniter


【解决方案1】:

我是忽视初学者错误的受害者。我将上传图片的方法命名为与上传类相同。因此,当我打电话去上课时,事情变得混乱了。吸取教训并解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2011-01-29
    • 2016-01-24
    • 2015-04-20
    • 2011-08-05
    相关资源
    最近更新 更多