2012-08-30 36 views
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 | 

回答

1

尝试像这样(如果你有文件数....你的情况是 “2”)

$this->load->model('upload_file', 'upload_model'); 
for($i=1;$i<=$numimg;$i++)      //in your case $numimg = 2; 
{ 
    if($this->upload->do_upload('File'.$i))  //File1,File2 are filenames 
    { 
     $img_data = $this->upload->data(); 
     $images[$i] = $img_data['file_name'];   
    } 
} 
$data['Image'] = implode(',',$images); 
$this->upload_model->upload_file($data); 

在你的模型:

public function upload_file2($data) 
{ 
    $data = array('file_name' => $data['Image']); 
    $this->db->insert('images_table', $data); 
} 

这就是你想从数据库使用retrive它吧。如果

$images = explode(',',$data->file_name); 

它会给你一个图像名称的数组。

+0

非常感谢!我很快就会发布类似的问题,但在表单中添加文本字段和字段名称不那么方便..只是为了扩展我自己的知识。 – Staysee

+0

K肯定亲爱的朋友 – Gautam3164

+0

如果你有时间,我真的很感激你看看[this。](http://stackoverflow.com/questions/12199938/codeigniter-write-text-and-file-fields-from -form-to-same-row-in-database) – Staysee

相关问题