0
votes
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Upload extends CI_Controller 
 {
    public function __construct()
     {
        parent::__construct();

            $this->load->library('session');
            $this->load->helper('form');
            $this->load->helper('url');
            $this->load->library('upload');
            $this->load->helper(array('form', 'url'));
            $this->load->database();
            $this->load->library('form_validation');
    }

    public function index()
    {
        $this->load->view('index');
    }

    public function files()
    {
        $this->load->model('file_model','b');
        $this->load->view('files');
    }

    public function upload_file()
    {
        $status = "";
        $msg = "";
        $filename = 'product_pic';

        if(empty($_POST['title']))
        {
            $status = "error";
            $title = "Please Enter Title";
        }

        if($status != "error")
        {   
            $config['upload_path'] = 'img/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = 1024 * 8;
            $config['encrypt_name'] = true;
            $this->upload->initialize($config);
            if(!$this->upload->do_upload->('$filename'))
            {
                $status = 'error';
                $msg = $this->upload->display_errors('','');
            }
            else
            {
                $this->load->model('file_model');
                $data =$this->upload->data();
                $file_id =$this->file_model->insert_file($data['file_name'],$_POST['title']);
                if($file_id)
                {
                    redirect('upload/index');
                }
                else
                {
                    unlink($data['full_path']);
                    $status = "error";
                    $msg    = "Please try again";
                }

            }
            @unlink($_FILES[$filename]);
        }
            echo json_encode(array('status'=>$status,'msg'=>$msg));
    }
}

?>

this is my controller file

on uploading image it showing error as follows

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Upload::$do_upload

Filename: controllers/upload.php

Line Number: 48

Backtrace:

File: D:\xampp\htdocs\picture\application\controllers\upload.php Line: 48 Function: _error_handler

File: D:\xampp\htdocs\picture\index.php Line: 315 Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/upload.php

Line Number: 48

Backtrace:

File: D:\xampp\htdocs\picture\application\controllers\upload.php Line: 48 Function: _error_handler

File: D:\xampp\htdocs\picture\index.php Line: 315 Function: require_once

1
Are you using codeigniter 3 or 2?syanaputra
Did you try my answer?Mr. ED
i tried all steps as you said but not working stillRamakant Purohit
i am using codeigniter 3.1.0Ramakant Purohit
('$filename') on adding this it showing error A PHP Error was encountered Severity: Parsing Error Message: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' Filename: controllers/Upload.php Line Number: 48 Backtrace:Ramakant Purohit

1 Answers

0
votes

Try this

if(!$this->upload->do_upload($filename))

Instead of

if(!$this->upload->do_upload->('$filename')) // you had extra ->

Filename should be

controllers/Upload.php

Not

controllers/upload.php

http://www.codeigniter.com/user_guide/general/styleguide.html#file-naming

You do not need to close the controller with ?> also

If non of that works try renaming controller to a different name because there is a class upload all ready.