文件上传代码点火器..不工作

时间:2013-02-08 09:46:16

标签: codeigniter upload

这是我的控制器上传文件

$config['upload_path'] = './assets/images/b2b/banner-agent/';
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = "$banner2";
$this->load->library('upload', $config);
$this->upload->data();
$this->upload->do_upload();
$this->upload->initialize($config);

我的代码有什么问题吗?上传无效。

1 个答案:

答案 0 :(得分:4)

在初始化和设置上传类的配置变量之前,不能简单地调用do_upload方法。

您需要像这样修改代码:

$config['upload_path'] = './assets/images/b2b/banner-agent/';
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = TRUE;
$config['file_name'] = $banner2;
$this->load->library('upload'); //initialize
$this->upload->initialize($config); //Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class
$this->upload->do_upload(); // do upload
if($this->upload->do_upload()){
    $this->upload->data(); //returns an array containing all of the data related to the file you uploaded.
}

您也可以参考 Codeigniter wiki

<强> http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

希望这有帮助。