文件上传时codeigniter错误

时间:2019-02-08 03:13:38

标签: codeigniter image-upload

上传文件时出现问题,这是整个控制器代码: Controller screenshot 这是文件上传代码:

    $config['upload_path'] = './_uploads';
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
	
		$this->load->library('Upload');
		$this->upload->initialize($config);
	
		if (!$this->upload->do_upload('image')) {
			echo "failed";
		
		} else {
			echo "sucess";
		}

当我运行它时,它给我错误:

An Error Was Encountered
Resource 'upload' already exists and is not a CI_Upload instance

3 个答案:

答案 0 :(得分:1)

您是否使用此代码上传多张图片?如果您要使用多张图片,请在循环外加载上传库$this->load->library('upload')

如果没有多个图像,请提供带有方法名称的完整代码。

答案 1 :(得分:1)

您正在加载“上传”而不是“上传”(上传应为小写)。而且我还想做更多的改变

    $config['upload_path'] = './_uploads';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['file_name'] = 'filename.png'; //extension should be same as uploaded file

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if (!$this->upload->do_upload('image')) {
        echo "failed";

    } else {
        echo "sucess";
    }

希望对您有帮助。

答案 2 :(得分:0)

上传类必须已经在您的config /autoload.php文件中已经自动加载,因此重新初始化上传类将引发错误。