文件未在codeigniter中上传

时间:2014-01-09 18:01:55

标签: codeigniter

我知道这个问题可能已被问过几百万次了。我搜索并按照上传文件所需的步骤。但是当我尝试上传文件时,我收到错误,即没有选择要上传的文件。 我的控制器是:

function save(){
        $this->load->library('upload');
        $config = array();
        $config['upload_path'] = './assets/images/site_data/images/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size']      = '100';
        $config['file_name'] = time();
        $config['overwrite']     = FALSE;
        $this->upload->initialize($config);
        if($this->upload->do_upload()){
                $fInfo = $this->upload->data();
                echo '<pre>';
                print_r($fInfo);
                exit;
            }
        else 
            {
            echo  $this->upload->display_errors();
        }   
    }

我的观点是:

<form method="post" action="<?php echo base_url()?>books/save" enctype="multipart/form-data">   
    <input type="file" name="cover_photo" id="cover_photo">
    <input type="submit" name="save">
</form>  

任何人都可以告诉我我的代码有什么问题。如果我犯了错误,请原谅我。 感谢。

3 个答案:

答案 0 :(得分:2)

问题是该类要求文件的名称为userfile,如documentation所示。

更改文件输入的名称或将要使用的名称添加到do_upload调用。

后者的一个例子:

$field_name = "cover_photo";
$this->upload->do_upload($field_name)

答案 1 :(得分:0)

尝试以下代码并根据您的要求进行修改

function save()
{
    $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);
    }
}

答案 2 :(得分:0)

只需按照CI文件上传文档:

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