【发布时间】: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