【问题标题】:Codeigniter upload errorCodeigniter 上传错误
【发布时间】:2013-09-18 19:18:08
【问题描述】:

请帮帮我!我想用codeigniter上传一个文件,但这会返回错误。这是上传者:

class Uploader extends CI_Model
 {
    function __construct()
    {
        parent::__construct();

        $config['upload_path']   = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|exe|xls|doc|docx|xlsx|rar|zip';
        $config['max_size']      = '8192'; // kbytes
        $config['encrypt_name']  = TRUE;

        $this->load->library( 'upload', $config );
        $this->response = '';
        $this->success  = TRUE;
    }

    /**
     *  Triggers upload
     *  returns the file's details on success or false
     */
    function upload( $field_name )
    {
        if ( !$this->upload->do_upload( $field_name ) )
        {
            $this->success  = FALSE;
            $this->response = $this->upload->display_errors();
            return false;
        }
        else
        {
            $this->success  = TRUE;
            $this->response = $this->upload->data();
            return $this->response['file_name']; 
        }
    }
}

这是错误信息:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Downloads::$required_empty

Filename: core/Model.php

Line Number: 51

自动加载数据库配置已开启。请帮帮我。

【问题讨论】:

  • 你怎么打电话给Uploader->upload()Downloads 是什么?你在哪里尝试访问$required_empty

标签: codeigniter


【解决方案1】:

你有没有尝试过这样的事情

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }
}
?>

你可以在这里http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html获得更多信息

上传文件夹 您需要为上传的图像设置一个目标文件夹。创建一个 CodeIgniter 安装根目录下名为 uploads 和 将其文件权限设置为 777。

【讨论】:

    【解决方案2】:

    试试这个

    if($this->input->post())
    {
       $file_element_name = 'image';  //this is the name of your input file. for example "image"
       if ($_FILES['image']['name']!= "")
       {
          $config['upload_path'] = './uploads/';
          $config['allowed_types'] = 'gif|jpg|png|exe|xls|doc|docx|xlsx|rar|zip';
          $config['max_size']      = '8192'; 
          $config['remove_spaces']=TRUE;  //it will remove all spaces
          $config['encrypt_name']=TRUE;   //it wil encrypte the original file name
          $this->load->library('upload', $config);
    
          if (!$this->upload->do_upload($file_element_name))
          {
             $error = array('error' => $this->upload->display_errors());
             $this->session->set_flashdata('error',$error['error']);
             redirect('controller_name/function_name','refresh');
          }
          else
          {
             $data = $this->upload->data();
             return $data['file_name'];          
          }
          $this->session->set_flashdata('msg','success message');
          redirect('controller_name/function_name','refresh');
       }
       else
       {
            //if no file uploaded the do stuff here
       } 
    }
    

    如果您遇到任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 2017-10-22
      • 2018-09-01
      • 2017-10-04
      • 1970-01-01
      相关资源
      最近更新 更多