why the load function is not working in codeigniter

541 views Asked by At

This code is not working not only this but when ever i use the this->load->library('email') or something its showing error as

"message": "Undefined property: App\\Controllers\\Epicureser1::$load", 

this is the code which i used currently

$config['upload_path']          = './uploads/';
$config['allowed_types']        = 'gif|jpg|png';
$config['max_size']             = 100;
$config['max_width']            = 1024;
$config['max_height']           = 768;
$this->load->library('upload',$config);
1

There are 1 answers

2
marcogmonteiro On

The code your are showing there is not a part of Codeigniter 4 but Codeigniter 3.

That was the old way of uploading a file. If uploading a file is what you are trying to achived that can be done by using the IncomingRequest Class.

$files = $request->getFiles();

// Grab the file by name given in HTML form
if ($files->hasFile('uploadedFile'))
{
    $file = $files->getFile('uploadedfile');

    // Generate a new secure name
    $name = $file->getRandomName();

    // Move the file to it's new home
    $file->move('/path/to/dir', $name);

    echo $file->getSize('mb');      // 1.23
    echo $file->getExtension();     // jpg
    echo $file->getType();          // image/jpg
}

That is not really a Library. If by any chance what you're looking for is how to load libraries in Codeigniter 4, that is actually pretty easy. Let's say you want to use the Email Library.

First you should configure your email settings on app/Config/Email.php

To send an email your just need to access the email service:

$email = \Config\Services::email();

$email->setFrom('[email protected]', 'Your Name');
$email->setTo('[email protected]');
$email->setCC('[email protected]');
$email->setBCC('[email protected]');

$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');

$email->send();

Now let's say what you want is to create a custom library, for that you should create your class inside your app/Libraries folder.

<?php

namespace App\Libraries;

class Custom
{

    public function myFunction()
    {
        return true;
    }

}

Now to load this library so you can use it's functions you should do the following.

$custom = new \App\Libraries\Custom();
$giveMeTrue = $custom->myFunction();

Like Vilar said in one of his comments:

If you're using Codeigniter 4, the super object $this, which was used for loading every kind of library, models, etc... has been removed. You should take a look at codeigniter.com/user_guide/installation/upgrade_4xx.html even it's not fully complete it will guide you through main changes.