Welcome Guest, Not a member yet? Register   Sign In
How to Display Upload Errors
#1

[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:
&lt;?php if (isset($errors)) {
foreach ($errors as $error) {?&gt;
    <div class="error">&lt;?php echo $error; ?&gt;</div>
&lt;?php }
} ?&gt;
#2

[eluser]aquary[/eluser]
You forgot to return error in the process_upload() function.

By your code, I'd do a bit different...

Code:
function index(){
      ...
      ...
      ...
      $data=$this->process_upload();
      if (empty($data['error']) {
         redirect('upload_success');
      }
   }

   function process_upload(){
      $data['error']=NULL;
      // change all data['upload_error'] to $data['error']
      ...
      ...
      return $data;
   }
#3

[eluser]RMinor[/eluser]
After I did that my page and testimonial variables are undefined. Here is my new controller...

Code:
&lt;?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 ($data = $this->process_upload()) {
    redirect('upload_success');
   }
  }
  $this->load->view('upload_view', $data);
}

/**
  * Method to upload two images and create thumbnails of them.
  */
public function process_upload()
{
  $data['error']=NULL;
  $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['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['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 $data;
}

}
#4

[eluser]aquary[/eluser]
errr... no, it was not I meant.
Code:
if (!empty($_FILES['photo_two']['name'])) {
      $this->upload->initialize($config);
      if (!$this->upload->do_upload('photo_two')) {
         // Process_upload() should return $data with errors array, which is empty by default. You are returning FALSE.
         // This is one thing you are't understand yet, you got undefined error because the $data['error'] was not defined in index() function.
         // Please remember that you are using $data which is a local data exists only inside this process_upload() method.  You must return the $data to use it outside.
         // If you return FALSE here, you will loose the error message.
      
         $data['error'][] = $this->upload->display_errors();
      } else {
         // skipped
      }
  }
  // Check if there are any error before call the addModel()
  if(empty($data['error'])){
     $this->Upload_model->addModel($db_info);
     return TRUE; // return TRUE for checking outside
   }
   else
     return $data; // return $data to the outside

and in the index(), since the returned value is either TRUE, or $data with errors, you could check with triple equal:
Code:
$data=$this->process_upload();
  if ($data===TRUE) {
    redirect('upload_success');
   }





Theme © iAndrew 2016 - Forum software by © MyBB