提问者:小点点

在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函数:\u错误\u处理程序

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

遇到一个PHP错误

严重性:通知

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

文件名:controllers/upload。php

行号: 48

回溯:

文件:D:\xampp\htdocs\picture\application\controllers\upload。php行:48函数:\u错误\u处理程序

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


共1个答案

匿名用户

试试这个

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

您不需要使用关闭控制器?

如果不起作用,请尝试将控制器重命名为其他名称,因为有一个类upload all ready。