Codeigniter上传错误

时间:2013-09-18 19:18:08

标签: codeigniter

请帮帮我!我想上传一个带有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

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

2 个答案:

答案 0 :(得分: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
   } 
}

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

答案 1 :(得分:0)

你试过这样的事吗

<?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。