codeigniter上传表单不起作用

时间:2016-09-28 04:04:28

标签: forms codeigniter file-upload

我从未使用过代码点火器,而且我试图制作包含图片上传输入的快速管理表单。我的管理员表单已启动并且有一个我可以到达的路由/网址,但保存功能无法正常工作。我点击提交按钮时收到404错误。

我认为问题在于views / admin / Dashboard.php中的form_open_multipart(' dashboard_save / do_upload')。我不认为正在达到do_upload功能。我出错了什么想法?

*新细节:我可以使用索引函数通过form_open_multipart(' dashboard_save')访问我的控制器...但我无法覆盖任何其他功能,例如form_open_multipart(' dashboard_save / upload'),使用控制器dashboard_save中的上传功能。

控制器

控制器/管理/ Dashboard_save.php

<?php

   class Dashboard_save extends CI_Controller
   {

      public function __construct()
      {
         parent::__construct();
         $this->load->helper(array('form', 'url'));
      }

      public function index()
      {
        // die('got here 2');
         $this->load->view('admin/dashboard_view', array('error' => ' ' ));
      }

      public function do_upload()
      {
        // die('got here!!');
         $config['upload_path']   = './uploads/';
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size']      = 100;
         $config['max_width']     = 1024;
         $config['max_height']    = 768;
         $this->load->library('upload', $config);

         if ( ! $this->upload->do_upload('userfile'))
         {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('dashboard_view', $error);
         }

         else
         {
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('dashboard_save', $data);
         }
      }
   }
?>

视图

视图/管理/ Dashboard.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
  <div id="main-wrapper">
    <div id="main-container">
      <div id="main-body">
        <div id='main-form-body'>
          <p>Configure front end here.</p>
          <div id='admin-form-container'> 
            <?php echo form_open_multipart('dashboard_save/do_upload');?>
            <form id="admin-form" method="" action="">
              <div class='form-field'>
                <div class='label-wrapper'><label for='main_img'>Main Image</label></div>
                <input type = "file" name = "userfile" size = "20" />
              </div>
            <div class='form-field'>
              <button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

视图/管理/ Dashboard_save.php

<html>
   <head><title>Dashboard Save</title></head>
   <body>
      <h3>testing dashbord submit</h3>
      <ul>
         <?php foreach ($upload_data as $item => $value):?>
         <li><?php echo $item;?>: <?php $value;?></li>
         <?php endforeach; ?>
      </ul>
      <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
   </body>
</html>

2 个答案:

答案 0 :(得分:1)

您好先检查您的do_upload函数是否已被调用。如果是,那么请检查您是否已加载上传库。如果不是这样,请上传库,而不是使用下面的代码

<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
  <div id="main-wrapper">
    <div id="main-container">
      <div id="main-body">
        <div id='main-form-body'>
          <p>Configure front end here.</p>
          <div id='admin-form-container'>        
            <form id="admin-form" method="post" action="<?php echo base_url()?>/dashboard_save/do_upload" enctype="multipart/form-data">
              <div class='form-field'>
                <div class='label-wrapper'><label for='main_img'>Main Image</label></div>
                <input type = "file" name = "userfile" size = "20" />
              </div>
            <div class='form-field'>
              <button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

答案 1 :(得分:1)

您使用了form标记的两倍。这就是它采取最后一个并显示错误的原因。删除一个FORM标记。这是您修改的视图文件代码

<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<body>
  <div id="main-wrapper">
    <div id="main-container">
      <div id="main-body">
        <div id='main-form-body'>
          <p>Configure front end here.</p>
          <div id='admin-form-container'> 
            <?php $attributes = array('id' => 'admin-form'); echo form_open_multipart('dashboard_save/do_upload', $attributes);?>
              <div class='form-field'>
                <div class='label-wrapper'><label for='main_img'>Main Image</label></div>
                <input type = "file" name = "userfile" size = "20" />
              </div>
            <div class='form-field'>
              <button id="submit-search" type="submit" class="button" title="Submit" value = "upload">Submit</button>
            </div>
          <?php echo form_close();?>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

没有测试。我删除了form open&amp;您手动编写的关闭标记,同时仅使用form_open_multipart()&amp; form_close()用于完成相同的任务