【问题标题】:Error Showing on file upload in codeigniter在codeigniter中显示文件上传错误
【发布时间】:2017-01-16 00:36:25
【问题描述】:
<?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 功能: 需要一次

遇到了 PHP 错误

严重性:通知

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

文件名:controllers/upload.php

行号:48

回溯:

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

文件:D:\xampp\htdocs\picture\index.php 行:315 功能: 需要一次

【问题讨论】:

  • 您使用的是 codeigniter 3 还是 2?
  • 你试过我的答案了吗?
  • 我按照你说的所有步骤都试过了,但还是不行
  • 我正在使用 codeigniter 3.1.0
  • ('$filename') 添加此内容时显示错误 A PHP Error was enough '{' 或 '$' 文件名:controllers/Upload.php 行号:48 回溯:

标签: codeigniter


【解决方案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

你也不需要用?&gt;关闭控制器

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多