[eluser]RMinor[/eluser]
Hi everyone. I am happy to report that I have come up with a solution to accomplish what I want. I will post it below for anyone who ever needs to upload multiple photos and create thumbnails of them. Enjoy!
Controller:
Code:
public function index()
{
// Retrieve page information
$data['page_info'] = $this->Global_model->pageInfo($this->_page);
// Retrieve number of photo fields we need
$data['photo_fields'] = $this->_photos;
// Load the form helper
$this->load->helper('form');
// Check if the submit button was pressed
if ($this->input->post('upload')) {
// Load the form validation library
$this->load->library('form_validation');
// Set form validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|is_unique[person.person_name]');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[person.person_email]');
$this->form_validation->set_rules('phone', 'Phone Number', 'trim|required|is_unique[person.person_phone]');
$this->form_validation->set_rules('age', 'Age Check', 'trim|required');
// If form validation fails
if ($this->form_validation->run() == FALSE) {
// If form validation is successful
} else {
// Insert person information into the database
$name = $this->input->post('name');
$email = $this->input->post('email');
$phone = $this->input->post('phone');
$age_check = $this->input->post('age');
$event_id = $this->_event_id;
$ip = $_SERVER['REMOTE_ADDR'];
if ($person_id = $this->Person_model->initialSubmit($name, $email, $phone, $age_check, $event_id, $ip)) {
// Load the upload library
$this->load->library('upload');
// Configure the upload library
$config['upload_path'] = './photos/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '5120'; // 5MB
$config['max_width'] = '720';
$config['max_height'] = '720';
// Retrieve number of items in the files array
$photos = count($_FILES);
// Loop through each photo uploaded
for ($i = 0; $i < $photos; $i++) {
// Initialize the upload library
$this->upload->initialize($config);
// If upload fails
if (!$this->upload->do_upload('photo' . $i)) {
// Retrieve the errors
$data['upload_errors'] = array('error' => $this->upload->display_errors());
// If the upload was successful
} else {
// Load the image library
$this->load->library('image_lib');
// Assign uploaded photo information to a variable
$photo_info = $this->upload->data();
// Configure the image library
$config_thumb['create_thumb'] = TRUE;
$config_thumb['maintain_ratio'] = TRUE;
$config_thumb['width'] = 100;
$config_thumb['height'] = 100;
$config_thumb['source_image'] = $photo_info['full_path'];
// Assign the photo filename
$photo = $photo_info['file_name'];
// Initialize the image library
$this->image_lib->initialize($config_thumb);
// If resize was successful
if ($this->image_lib->resize()) {
// Assign the thumbnail filename
$thumbnail = $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext'];
// Insert photo information into the database
if ($this->Person_model->insertPhoto($photo, $thumbnail, $person_id)) {
$data['success'] = TRUE;
}
// Clear current data from the image library
$this->image_lib->clear();
}
}
}
}
}
}
// Load the required view and data
$this->load->view('submit_view', $data);
}
View (form only):
Code:
<form method="post" action="" name="contactform" id="contactform" class="contact-form" enctype="multipart/form-data" />
<div>
<label for="name">Full Name</label>
<input name="name" type="text" id="name" size="30" value="" />
</div>
<div>
<label for="email">Email Address</label>
<input name="email" type="text" id="email" size="30" value="" />
</div>
<div>
<label for="phone">Phone Number</label>
<input name="phone" type="text" id="subject" value="" />
</div>
<?php for ($i = 0; $i < $photo_fields; $i++) { ?>
<div>
<label for="photo<?php echo $i; ?>">Photo</label>
<input name="photo<?php echo $i; ?>" type="file" id="photo<?php echo $i; ?>" value="" />
</div><br />
<?php } ?>
<div>
<label for="age">I verify that I am at least 18 years of age</label>
<input name="age" type="radio" id="age" value="Yes" /> Yes
<input name="age" type="radio" id="age" value="No"> No
</div><br />
<input type="submit" class="form-submit" name="upload" id="submit" value="Upload" />
</form>
Of course you will want to add any success messages that you want to display. If anyone sees anything I could've done better please let me know. Otherwise, I hope this helps someone out!