简体   繁体   中英

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

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); try to check this documentation hope it will help you. https://www.codeigniter.com/userguide3/libraries/file_uploading.html

The error indicates that the upload library is not loaded. 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'] = array('upload');

Other libraries can be loaded at the same time. See the comments in autoload.php for more details.

Add upload library before you could call do_upload with config

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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