文件上传类codeigniter中的未定义变量$ error

时间:2014-02-18 07:13:53

标签: codeigniter file-upload

我正在尝试使用Codeigniter上传类将pdfs上传到一个正常工作的文件夹。但是,错误变量显示为未定义,因此我无法看到错误,如果我上传错误的文件。请建议。

以下是我的观点,

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

和我的控制器功能,

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

感谢。

2 个答案:

答案 0 :(得分:1)

function add_pdf(){

中添加此行
$data["error"] = "";

并在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);
}

答案 1 :(得分:0)

变量$ error是本地的,您没有将此变量传递给您的视图。将您的代码更改为此

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

              }