I'm new at CodeIgniter and just ran through a NetTuts intro lesson. 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. 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. I've already added a 'file' column to the Page table.
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. These are the configurations I'd like to use, but I don't know how to integrate this to work with my Page type.
$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.
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();?>
答案 0 :(得分:0)
答案 1 :(得分: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());
}