2

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.

0

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but when i am doing this the button for browsing files itself vanishes(input type='file') and only upload button(input type='upload') shows.
0

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

              }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.