Welcome Guest, Not a member yet? Register   Sign In
Problem with Uploading two images, re-sizing them, and saving them to the database
#1

[eluser]RMinor[/eluser]
Hi, I have an issue with uploading two images, re-sizing them, and saving them to the database. When I submit the form I get an error stating that 'photo_one, 'thumb_one', 'photo_two', and 'thumb_two' are undefined indexes. It's like they aren't even being passed. Can anybody see what's going on with my code below?

Controller:
Code:
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('photo_one', 'Photo', 'trim');
   $this->form_validation->set_rules('photo_two', '', 'trim');
   $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');
  $db_info = array();
  // Set configuration array for uploaded photo.
  $config['upload_path'] = 'models/';
  $config['allowed_types'] = 'gif|jpg|jpeg|png';
  $config['max_size'] = '2048';
  $config['max_width']  = '0';
  $config['max_height']  = '0';
  // 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'])) {
   if ($this->upload->do_upload('photo_one')) {
    $this->upload->initialize($config);
    $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'];
    $this->image_lib->clear();
   } 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'];
    $this->image_lib->clear();
   } else {
    $data['upload_error'][] = $this->upload->display_errors();
   }
  }
  $this->Model_model->submit($db_info);
}

Model:
Code:
public function submit($data)
{
  $result = $this->db->query("INSERT INTO model
   (model_name,
   model_stage_name,
   model_email,
   model_birth_month,
   model_birth_day,
   model_birth_year,
   model_photo_one,
   model_thumb_one,
   model_photo_two,
   model_thumb_two,
   model_age_check,
   model_ip)
  VALUES
   (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array(
    $this->input->post('name'),
    $this->input->post('stage_name'),
    $this->input->post('email'),
    $this->input->post('birth_month'),
    $this->input->post('birth_day'),
    $this->input->post('birth_year'),
    $data['photo_one'],
    $data['thumb_one'],
    $data['photo_two'],
    $data['thumb_two'],
    $this->input->post('age_check'),
    $this->input->post('ip')));
  if ($result) {
   return TRUE;
  }
  return FALSE;
}
#2

[eluser]RMinor[/eluser]
Bump for reply.
#3

[eluser]CroNiX[/eluser]
I would check the contents of $_FILES array when you upload. Do a print_r($_FILES). I think the way you are trying to access them might be wrong.

Also, these rules are unnecessary and might cause problems:
Code:
$this->form_validation->set_rules('photo_one', 'Photo', 'trim');
$this->form_validation->set_rules('photo_two', '', 'trim');
Since $_FILES aren't sent via $_POST.

Edit: It looks like you are only putting the values in the array that get sent to submit() if the file(s) got uploaded, but in upload() you save everything as though it exists.
#4

[eluser]RMinor[/eluser]
Okay I removed the validation rules and checked the $_FILES array. Everything is there as it should be for both file upload fields. Its like the code is just not grabbing the information for the second field.

What do you mean by your comment that you edited in?

Also, when I print_r($db_info) I get array values for photo_one and thumb_one, but nothing for photo_two or thumb_two.
#5

[eluser]RMinor[/eluser]
I made some minor changes, but still only get values in the $db_info array for photo_one and thumb_one. Anyone have any ideas why? My controller is below.

Code:
&lt;?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);
}

/**
  * Mehtod to upload two images, create thumbnails of them, and insert all four filenames into the database.
  * If we want to resize the original to a "resized" version we would need to unlink() the original.
  */
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);
}

}
#6

[eluser]CroNiX[/eluser]
One potential problem I see is that you are using common names that are used elsewhere. Example:

public function upload(){}

You are using $this->upload() to access that method, however, when you load the upload library, it also uses $this->upload(). Could potentially cause a problem there. Maybe call your method "process_upload" or something else.

I also wouldn't name a controller "model". It might work, but not the best practice. You should use names that are more unique and not so "common".

You say that the data for your 2nd image isn't showing up in your $db_info array. Is the image actually getting processed though?
#7

[eluser]RMinor[/eluser]
Thanks, I will rename those things in a couple minutes. As far as the second image, no it isn't getting processed. Only the first one is.

EDIT: Okay I just renamed my method and it worked now. I can't believe I overlooked that...such a beginner mistake. Anyway, I am going to apply some "best-practices" to this code and deploy it. Thanks so much!
#8

[eluser]RMinor[/eluser]
I guess I spoke too soon. I went home after work and tried it out again and it didn't work. The $_FILES array is perfect but my $db_info and $photo_info arrays are empty. I have no idea why. I'll post my controller again below.

Code:
public function process_upload()
{
    $this->load->library('upload');
    $this->load->library('image_lib');
  
    $config['upload_path'] = 'models/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '2048';
  
    $config_thumb['image_library'] = 'GD2';
    $config_thumb['create_thumb'] = TRUE;
    $config_thumb['maintain_ratio'] = TRUE;
    $config_thumb['width'] = 150;
    $config_thumb['height'] = 150;
    
    // print_r($_FILES);

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

    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();
        }
    }
    $this->Model_model->submit($db_info);
}




Theme © iAndrew 2016 - Forum software by © MyBB