[eluser]RMinor[/eluser]
I am having a difficult time figuring out how to display the upload errors on my view. I figured putting them into an array and then passing that to the view would enable it work how I have it coded. However, when I try to upload a file that is too large I just get an undefined index "upload_error". My controller and view are below.
Controller...
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller {
protected $_page = 'upload';
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->model('Upload_model');
$data['page'] = $this->Global_model->pageInformation($this->_page);
$data['testimonials'] = $this->Upload_model->getTestimonials();
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$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('ip', 'IP Adress', 'trim|required|valid_ip');
if($this->form_validation->run() == FALSE) {} else {
if ($this->process_upload()) {
redirect('upload_success');
} else {
$data['errors'] = $data['upload_error']; // the $data['upload_errror'] is coming back undefined
}
}
$this->load->view('upload_view', $data);
}
/**
* Method to upload two images and create thumbnails of them.
*/
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['create_thumb'] = TRUE;
$config_thumb['maintain_ratio'] = TRUE;
$config_thumb['width'] = 150;
$config_thumb['height'] = 150;
if (!empty($_FILES['photo_one']['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload('photo_one')) {
$data['upload_error'][] = $this->upload->display_errors();
return FALSE;
} else {
$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();
}
}
if (!empty($_FILES['photo_two']['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload('photo_two')) {
$data['upload_error'][] = $this->upload->display_errors();
return FALSE;
} else {
$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();
$this->image_lib->clear();
$db_info['thumb_two'] = $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext'];
}
}
$this->Upload_model->addModel($db_info);
return TRUE;
}
}
View (This is only the code to display the errors)...
Code:
<?php if (isset($errors)) {
foreach ($errors as $error) {?>
<div class="error"><?php echo $error; ?></div>
<?php }
} ?>