1

请帮我!我想用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

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

4

2 回答 2

4

尝试这个

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
   } 
}

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

于 2013-09-19T08:02:11.117 回答
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。

于 2013-09-18T19:36:17.717 回答