Learner_Programmer
Learner_Programmer

Reputation: 1299

undefined variable $error in file uploading class codeingiter

I am trying to use Codeigniter upload class to upload pdfs to a folder which is working fine.However, the error variable is showing as undefined and because of this I could not see the error if iam uploading a wrong file. Please suggest.

Here is my view ,

             <?php echo $error;?>

             <?php echo form_open_multipart('admin/admin_elements/do_upload_pdf');?>

             <input type="file" name="pdf" class="btn" />

             <br /><br />

             <input type="submit"  class="btn btn-info" value="upload" />

             <?php echo form_close(); ?>

and my controller functions,

      function add_pdf(){

                  $data['main_content'] ='admin/elements/add_pdf';
                  $this->load->view('includes/template', $data);          

      }

      function do_upload_pdf(){

                  $config['upload_path'] = './uploads/';
                  $config['allowed_types'] = 'pdf';
                  $config['max_size'] = '10000';

                  $this->load->library('upload', $config);

                  if ( ! $this->upload->do_upload('pdf'))
                  {
                     $error = array('error' => $this->upload->display_errors());

                     $data['main_content'] ='admin/elements/add_pdf';
                     $this->load->view('includes/template', $data);

                  }
                  else
                  {
                     $chapter_id=$this->session->userdata('chapter_id');
                     redirect("/admin/elements/".$chapter_id);
                 }
}

Thanks.

Upvotes: 2

Views: 1060

Answers (2)

Sanoob
Sanoob

Reputation: 2474

The variable $error is local, and your not passing this variable to your view . Change your code to like this

if ( ! $this->upload->do_upload('pdf'))
              {
                 $data['error'] = array('error' => $this->upload->display_errors());
                //  ^^^^^^^^^^
                 $data['main_content'] ='admin/elements/add_pdf';
                 $this->load->view('includes/template', $data);

              }

Upvotes: 0

Kumar V
Kumar V

Reputation: 8830

Add this line in function add_pdf(){

$data["error"] = "";

and in do_upload_pdf()

if ( ! $this->upload->do_upload('pdf'))
{
 $data['error'] = $this->upload->display_errors();
 $data['main_content'] ='admin/elements/add_pdf'; 
 $this->load->view('includes/template', $data);
}

Upvotes: 1

Related Questions