1
我有一个带有两个文件上传字段的上传表单。我试图上传文件,然后将文件名写入数据库中的同一行。除了文件名被写入他们自己的行之外,我有一切工作。我猜测这是因为我正在对该模型进行两次不同的调用,但我不确定如何在一次调用中执行它,因为它们每个都有自己的“if”语句。CodeIgniter - 从多个图像向数据库写入文件名
为了清楚我的问题。如何将两个上传文件的名称写入数据库中的同一行?
任何想法都会很棒。这是我正在使用的代码。谢谢。
查看:
<?php echo form_open_multipart('upload_controller'); ?>
<p>
<input type="file" name="userfile">
</p>
<p>
<input type="file" name="userfile1">
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>
控制器:
if (isset($_POST['submit'])) {
$this->load->library('upload');
if (!empty($_FILES['userfile']['name'])) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('userfile')) {
$img = $this->upload->data();
$file_name = $img['file_name'];
$this->load->model('upload_file', 'upload_model');
$this->upload_model->upload_file1($file_name);
}
}
if (!empty($_FILES['userfile1']['name'])) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
// Upload file 2
if ($this->upload->do_upload('userfile1')) {
$img = $this->upload->data();
$file_name = $img['file_name'];
$this->load->model('upload_file', 'upload_model');
$this->upload_model->upload_file2($file_name);
} else {
echo $this->upload->display_errors();
}
}
} else {
$this->load->view("upload_form");
}
型号:
public function upload_file1($file_name) {
$data = array('file1' => $file_name);
$this->db->insert('images_table', $data);
}
public function upload_file2($file_name) {
$data = array('file2' => $file_name);
$this->db->insert('images_table', $data);
}
数据库行:
| id | file1 | file2 |
非常感谢!我很快就会发布类似的问题,但在表单中添加文本字段和字段名称不那么方便..只是为了扩展我自己的知识。 – Staysee
K肯定亲爱的朋友 – Gautam3164
如果你有时间,我真的很感激你看看[this。](http://stackoverflow.com/questions/12199938/codeigniter-write-text-and-file-fields-from -form-to-same-row-in-database) – Staysee