简体   繁体   English

Codeigniter文件上传代码不起作用

[英]Codeigniter file upload code not working

Controller Code: 控制器代码:

if(isset($_FILES['imageupload']) && $_FILES['imageupload']['size'] > 0){
    echo 'inside if';
    if ( ! $this->upload->do_upload('imageupload'))
    {
    echo 'inside if';
    $error = array('error' => $this->upload->display_errors());
    }
    else
    {
    echo 'inside else';
    $upload_data = $this->upload->data(); 
    $file_name[] = $upload_data['file_name'];
    } 
}

View Code: 查看代码:

<input type="file" class="default" id="imageupload" name="imageupload">

ERROR: Call to a member function do_upload() on a non-object 错误:在非对象上调用成员函数do_upload()

Hoow can I resolve this error please help me. 我可以解决此错误,请帮助我。

Did you load the library upload on your controller? 您是否将库上传文件加载到控制器上? I guess the problem of your code is lacking of this code $this->load->library('upload', $config); 我想您的代码的问题是缺少此代码$this->load->library('upload', $config); try to check this documentation hope it will help you. 尝试查看此文档,希望对您有所帮助。 https://www.codeigniter.com/userguide3/libraries/file_uploading.html https://www.codeigniter.com/userguide3/libraries/file_uploading.html

The error indicates that the upload library is not loaded. 该错误表明未加载upload库。 You need to load the upload library somewhere and in the constructor of the controller is one possible choice. 您需要将上传库加载到某个地方,并且在控制器的构造函数中是一种可能的选择。 Add the following to the controller code. 将以下内容添加到控制器代码中。

function __construct()
{
  parent::__construct();
  $this->load->library('upload');
}

The other place to load this library is in application/config/autoload.php using the $autoload['libraries'] item. 另一个加载该库的地方是使用$autoload['libraries']项目在application / config / autoload.php中

$autoload['libraries'] = array('upload');

Other libraries can be loaded at the same time. 可以同时加载其他库。 See the comments in autoload.php for more details. 有关更多详细信息,请参见autoload.php中的注释。

Add upload library before you could call do_upload with config 添加上载库之前,您可以使用config调用do_upload

$config['upload_path'] = '/path/';
$config['allowed_types'] = '*';
$config['file_name'] = 'file_name';
$config['overwrite'] = TRUE|FALSE;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM