CodeIgniter正在向现有编辑函数添加文件上载


CodeIgniter Adding File Upload to Existing Edit Function

我是CodeIgniter的新手,刚刚上了一堂NetTuts入门课。我现在正努力在我所学的基础上再接再厉。

如何将文件上传字段添加到现有页面类型?下面是我在页面控制器中的编辑功能。这是一个基本页面,包含标题、Slug、正文、模板、Parent_ID。如何将文件上传数据添加到其中?

我想将文档上传到"Uploads"目录,并将其文件路径保存到现有的Page表中。我已经在Page表中添加了一个"文件"列。

    public function edit ($id = NULL)
{
    // Fetch a page or set a new one
    if ($id) {
        $this->data['page'] = $this->page_m->get($id);
        count($this->data['page']) || $this->data['errors'][] = 'page could not be found';
    }
    else {
        $this->data['page'] = $this->page_m->get_new();
    }
    // Pages for dropdown
    $this->data['pages_no_parents'] = $this->page_m->get_no_parents();
    // Set up the form
    $rules = $this->page_m->rules;
    $this->form_validation->set_rules($rules);
    // Process the form
    if ($this->form_validation->run() == TRUE) {
        $data = $this->page_m->array_from_post(array(
            'title', 
            'slug', 
            'body', 
            'template', 
            'parent_id'
        ));
        $this->page_m->save($data, $id);
        redirect('admin/page');
    }
    // Load the view
    $this->data['subview'] = 'admin/page/edit';
    $this->load->view('admin/_layout_main', $this->data);
}

我看过CodeIgniter的文件上传类。这些是我想使用的配置,但我不知道如何将其集成到我的Page类型中。

        $config['upload_path'] = './uploads';
        $config['allowed_types'] = 'pdf';
        $config['max_size'] = '1000000';
        $this->load->library('upload', $config);

如果我能在如何将数据放入数据库和文件放入目录的问题上找到正确的方向,我相信我能找到如何在视图中显示所有数据的方法。

谢谢!

**EDIT(现在有表单输出)**

<?php echo form_open(); ?>
<table class="table">
    <tr>
        <td>Parent</td>
        <td><?php echo form_dropdown('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id); ?></td>
    </tr>
    <tr>
        <td>Template</td>
        <td><?php echo form_dropdown('template', array('page' => 'Page', 'news_archive' => 'News Archive', 'homepage' => 'Homepage'), $this->input->post('template') ? $this->input->post('template') : $page->template); ?></td>
    </tr>
    <tr>
        <td>Title</td>
        <td><?php echo form_input('title', set_value('title', $page->title)); ?></td>
    </tr>
    <tr>
        <td>Slug</td>
        <td><?php echo form_input('slug', set_value('slug', $page->slug)); ?></td>
    </tr>
    <tr>
        <td>Body</td>
        <td><?php echo form_textarea('body', set_value('body', $page->body), 'class="tinymce"'); ?></td>
    </tr>
    <tr>
        <td>File Upload</td>
        <td><?php echo form_upload('userfile', set_value('userfile', $page->userfile)) ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
    </tr>
</table>
<?php echo form_close();?>

我不确定,但在保存记录之前添加了这段代码。

$this->load->library('upload', $config);
$this->upload->initialize($config);                             
if(!$this->upload->do_upload('userfile'))                       
{
  $error = array('error' => $this->upload->display_errors());   
  $this->load->view('upload_form', $error);                     
}
else
{
  $data = array('upload_data' => $this->upload->data());        
  //do stuff
}

在HTML中,请确保更改以下内容:

<?php echo form_open(); ?>

到此:

<?php echo form_open_multipart();?>

为了让PHP读取您提交的$_FILES。一旦你这样做,处理上传如下:

$this->load->library('upload');
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->upload->initialize($config);
if ($this->upload->do_upload("userfile")) {
   //your success code
   $upload_data = $this->upload->data();
   $data_to_save['file_name'] = $upload_data['file_name'];
} else {
   //your failure code
   var_dump($this->upload->display_errors());
}