无法以codeigniter文本格式上传文件

时间:2014-01-04 19:32:07

标签: php forms codeigniter

我有一个可用的文本表单,我有一个文件上传表单,使用codeigniter docs中的确切示例,但我似乎无法将两者放在一起。我想要一个允许文本和文件上传的表单。文本应该转到数据库,文件存储到文件路径保存在DB中。

我尝试添加

enctype="multipart/form-data"

到我的文本表单和文件上传表单元素。表单操作目前是以下方法(见下文)。我试图将codeigniter doc中的方法添加到下面的方法以允许文件上传,但遗憾的是这不起作用。我究竟做错了什么?我怀疑我需要调用$ _file?

public function save()
{
    $project_id = $this->input->post('project_id');
    $id = $this->input->post('task_id');

    if($this->input->post('cancel') !== FALSE) {
        if($id)
            redirect('task/view/'.$project_id.'/'.$id);
        else
            redirect('project/tasks/'.$project_id);
    }

    $this->load->library('form_validation');

    $this->form_validation->set_rules('title', 'Title', 'trim|required');
    etc....

    if($this->form_validation->run() === false)  {
        $this->error = true;

        if ($id)
            $this->edit ($project_id, $id);
        else
            if($project_id)
                $this->add ($project_id);
            else
                redirect('dashboard');

        return;
    }

    $this->load->model('task_model');

    $sql_data = array(
        'project_id'  => $project_id,
        etc...
    );
    if ($id)
        $this->task_model->update($this->input->post('project_id'), $id, $sql_data);
    else
        $id = $this->task_model->create($sql_data);

    //add in upload function here
    $config['upload_path'] = './upload/cell/';
    $config['allowed_types'] = 'gif|jpg|png|tif';
    $config['max_size'] = '1000';
    $config['max_width']  = '2024';
    $config['max_height']  = '1768';

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

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

        $this->load->view('upload_forms', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
    }
            // end of added uploader code

    redirect('project/tasks/'.$this->input->post('project_id'));
}

1 个答案:

答案 0 :(得分:1)

尝试使用字段名称

 $this->upload->do_upload('upload');

而不是

$this->upload->do_upload();

HTML中的字段可能是

<input type="file" name="upload"/>

根据Codeignitor文档:

If you would like to set your own field name simply pass its 
value to the do_upload function

还要确保您的上传路径有效且可写

查看更多信息here