【问题标题】:Error in codeigniter while file uploading文件上传时codeigniter出错
【发布时间】:2019-02-08 03:13:38
【问题描述】:

上传文件时遇到问题,这是整个控制器代码: 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

【问题讨论】:

  • 在您的问题中添加完整的代码。

标签: codeigniter image-upload


【解决方案1】:

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

如果不是多张图片,请提供带有方法名称的完整代码。

【讨论】:

  • 不,它只是一个文件上传
  • 我在原帖上添加了整个控制器代码,请检查一次@Danish Ali
【解决方案2】:

您正在加载“上传”而不是“上传”(上传应该小写)。我也想再做些小改动

    $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";
    }

希望对你有帮助。

【讨论】:

  • 问题在于上传库我尝试了很多不同的方法,但没有任何改变
  • 能否按函数打印错误:print_r($this->upload->display_errors());这样我可以更好地理解。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 2012-03-07
    • 2018-01-25
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多