CodeIgniter doesn't load upload library

7.7k views Asked by At

I'm having a problem with loading a CodeIgniter library,

I've already built three (3) websites with CodeIgniter, so I consider myself familiar with this framework.

The problem is only with the upload lib. I've tried with multiple functions and it still doesn't work.

I've even tried with the CodeIgniter example

In google I can't find any answers, does anyone have an idea what it could be?

class Admin extends CI_Controller {

public function __construct(){
    parent::__construct();
    $this->load->library('session');
    $this->load->library('v');
    $this->load->model('admin_model');
}

public function upload_a_thing($set = null){
    $this->load->helper(array('form', 'url'));
    if(!$set){
        $this->load->view('admin/upload_a_thing');
    }else{
        $this->load->library('Upload');
        if (!$this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
        } else {

            $file = $this->upload->data();
            //          $product = $this->admin_model->get_products($id);
            $newFilePath = $config['upload_path'].'try.jpg';
            $file['file_name'] = str_replace(" ","_",$file['file_name']);
            rename($config['upload_path'].$file['file_name'], $newFilePath);
        }   
    }
}   

CodeIgniter Error

 Undefined property: Admin::$upload

PHP error

Fatal error: Call to a member function do_upload() on a non-object 

Edited for Spary

        $config['upload_path'] = './media/imgs/products/';
        $this->load->library('upload',$config);
        if (!$this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
        }
3

There are 3 answers

1
Verhaeren On BEST ANSWER

If your form looks like this:

<input type="file" class="input" name="logo"  />

then call do_upload() like this:

 do_upload('logo');

If it looks like this:

 <input type="file" class="input" name="userfile" />

then call it like this:

 do_upload();
0
Dexter On

do something like this

if (!$this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
        } 
4
Sparky On

Your code...

$this->load->library('Upload');

As per all examples in the documentation, upload should be in all lower-case and most likely the reason the library is not loading.


Additionally, if your $config array is missing, you're not following the example code in the documentation:

$config['upload_path'] = './uploads/';   // <- preferences
$this->load->library('upload', $config); // <- load library and set configuration

OR

$this->load->library('upload');         // <- load library

$config['upload_path'] = './uploads/';  // <- preferences
$this->upload->initialize($config);     // <- set configuration

OR

// 'upload' library is "auto-loaded"    // <- load library

$config['upload_path'] = './uploads/';  // <- preferences
$this->upload->initialize($config);     // <- set configuration

From the chart of preferences, they all seem optional, except for upload_path...

Default Value: None

Description: The path to the folder where the upload should be placed. The folder must be writable and the path can be absolute or relative.

I'm not sure how CodeIgniter could know where to upload the file when there's no upload_path because there is no default and your entire configuration array is missing.