3

这是在我的控制器中用于文件上传

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

我的代码有什么问题吗?上传不工作。

4

1 回答 1

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

希望这可以帮助。

于 2013-02-08T10:13:19.100 回答