在codeigniter中显示文件上载时出错

时间:2016-09-08 07:21:24

标签: codeigniter

<?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));
    }
}

?>

这是我的控制器文件

上传图片时显示错误如下

  

遇到PHP错误

     

严重性:注意

     

消息:未定义属性:CI_Upload :: $ do_upload

     

文件名:controllers / upload.php

     

行号:48

     

回溯:

     

文件:D:\ xampp \ htdocs \ picture \ application \ controllers \ upload.php行:   48功能:_error_handler

     

文件:D:\ xampp \ htdocs \ picture \ index.php行:315功能:   require_once

     

遇到PHP错误

     

严重性:注意

     

消息:尝试获取非对象的属性

     

文件名:controllers / upload.php

     

行号:48

     

回溯:

     

文件:D:\ xampp \ htdocs \ picture \ application \ controllers \ upload.php行:   48功能:_error_handler

     

文件:D:\ xampp \ htdocs \ picture \ index.php行:315功能:   require_once

1 个答案:

答案 0 :(得分:0)

试试这个

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

而不是

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

文件名应为

controllers/Upload.php

controllers/upload.php

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

您无需使用?>

关闭控制器

如果不起作用,请尝试将控制器重命名为其他名称,因为已经准备好上传类。