【问题标题】:Codeigniter 'You did not select a file to upload.'Codeigniter '您没有选择要上传的文件。'
【发布时间】:2014-10-05 19:13:36
【问题描述】:

我正在尝试使用 codeigniter 上传文件,无论我尝试了什么,我都会收到错误消息,即我实际上没有选择文件。

我的控制器看起来像这样

public function editHeader()
{
    $this->require_auth();      

    $config['upload_path'] = './files/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000KB';
            $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());

                var_dump($error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());
                echo('yeah!');
            }               
} 

而我的 html 只是一个 codeigniter 表单

<?foreach($header as $row) :?>
      <?=form_open_multipart('admin/editHeader');?>
         <input class="waitForLoad fadeInDown" type="file" name="userfile"  />
        <input type="submit" name="submit" value="Change" class="btn btn-success pull-right" />
      </form>
<?endforeach;?>

我已经尝试了正常的解决方案,例如增加max_size 以及将上传名称放在 do_upload 函数中,但似乎没有任何效果,我正要拔掉头发。任何帮助表示赞赏!

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    上传文件时需要使用form_open_multipart(),而不是form_open()

    【讨论】:

    • 即使我没有上传多个文件?这实际上不起作用......它仍然给我同样的错误信息
    • @zazvorniki 是的。多部分与文件的数量无关,并且是上传任何文件所必需的。来自文档 (ellislab.com/codeigniter/user-guide/libraries/…):By default the upload routine expects the file to come from a form field called userfile, and the form must be a "multipart type
    • 奇怪的是它仍然无法正常工作。你在 $_FILES 中看到了吗?
    【解决方案2】:

    你必须使用 &lt;?php echo form_open_multipart(path); ?&gt; 因为如果我们在表单中写入,我们会在控制器中获得 $_FILES。

    添加后,如果你 print_r($_FILES) 那么你会得到数组。

    在我们做图片上传或验证码相关的工作时需要用到

    【讨论】:

      猜你喜欢
      • 2014-03-05
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 2013-07-26
      • 2016-03-13
      相关资源
      最近更新 更多