简体   繁体   English

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

[英]CodeIgniter Adding File Upload to Existing Edit Function

I'm new at CodeIgniter and just ran through a NetTuts intro lesson. 我是CodeIgniter的新手,并且刚刚参加了NetTuts入门课程。 I'm now trying to build on what I've learned. 我现在正在尝试以我学到的知识为基础。

How would I be able to add a file upload field to an existing page type? 如何将文件上传字段添加到现有页面类型? Below is my Edit function from the Page Controller. 下面是我在页面控制器中的编辑功能。 It's a basic page with Title, Slug, Body, Template, Parent_ID. 这是带有标题,子标题,正文,模板,Parent_ID的基本页面。 How would I add the File upload data to it? 如何将文件上传数据添加到其中?

I want to upload the document to an 'Uploads' directory, and save its File Path into the existing Page table. 我想将文档上传到“上传”目录,并将其文件路径保存到现有的Page表中。 I've already added a ' file ' column to the Page table. 我已经在Page表中添加了一个' file '列。

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

I've looked at the File Upload Class by CodeIgniter. 我看了CodeIgniter的File Upload Class。 These are the configurations I'd like to use, but I don't know how to integrate this to work with my Page type. 这些是我要使用的配置,但是我不知道如何将其集成到我的Page类型中。

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

If I can get pointed in the right direction on how to get the data into my DB and the file into my directory, I believe I can figure out how to display it all in my View. 如果我能正确地指出如何将数据放入数据库以及将文件放入目录,我相信我可以弄清楚如何在View中显示所有内容。

Thank you! 谢谢!

** EDIT (now with form output) ** **编辑(现在带有表单输出)**

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

I am not sure but added this code before save the record. 我不确定,但是在保存记录之前添加了此代码。

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

In your HTML, make sure you change this: 确保在HTML中进行以下更改:

<?php echo form_open(); ?>

to this: 对此:

<?php echo form_open_multipart();?>

In order for PHP to read the $_FILES you submit. 为了让PHP读取您提交的$_FILES Once you do that, process the upload like this: 完成后,请按以下方式处理上传:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM