首页 文章

CodeIgniter将文件上载添加到现有编辑功能

提问于
浏览
1

我是CodeIgniter的新手,刚刚完成了NetTuts入门课程 . 我现在正在努力 Build 我所学到的东西 .

如何将文件上载字段添加到现有页面类型?以下是页面控制器的编辑功能 . 这是一个 Headers ,Slug,Body,Template,Parent_ID的基本页面 . 如何将文件上传数据添加到它?

我想将文档上传到'Uploads'目录,并将其文件路径保存到现有的Page表中 . 我_Page46193_ file '列到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);

如果我可以指出如何将数据导入我的数据库以及将文件存入我的目录的正确方向,我相信我可以弄清楚如何在我的视图中显示它 .

谢谢!

编辑(现在有表格输出)

<?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();?>

2 回答

  • 0

    我不确定但在保存记录之前添加了此代码 .

    $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
    }
    
  • 0

    在您的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());
    }
    

相关问题