使用CodeIgniter

时间:2017-12-01 12:56:14

标签: php codeigniter upload

帮我更新数据(图像文件),文件有4种输入类型

如果我更新图像1和2,图像3和4将与图像2具有相同的名称

但如果我再次更新到图像3和4,图像1和2将丢失

信息:图片1:$lname,图片2:$mname,图片3:$nname和图片4:$oname

有我的 的控制器

$this->upload->do_upload('k');
$hasil = $this->upload->data();
$k = $this->upload->data();
$kname = $k['file_name'];

if ($lname == '') {
    $this->upload->do_upload('l');
    $l = $this->upload->data();
    $lname = $l['file_name'];
}
if ($mname == '') {
    $this->upload->do_upload('m');
    $m = $this->upload->data();
    $mname = $m['file_name'];
}
if ($nname == '') {
    $this->upload->do_upload('n');
    $n = $this->upload->data();
    $nname = $n['file_name'];
}
if ($oname == '') {
    $this->upload->do_upload('o');
    $o = $this->upload->data();
    $oname = $o['file_name'];
}
$data = array('gambar' => $hasil['file_name'],
    'gambar1' => $lname,
    'ket_gambar1' => $this->input->post('c'),
    'gambar2' => $mname,
    'ket_gambar2' => $this->input->post('d'),
    'gambar3' => $nname,
    'ket_gambar3' => $this->input->post('e'),
    'gambar4' => $oname,
    'ket_gambar4' => $this->input->post('f')
);

1 个答案:

答案 0 :(得分:1)

好吧,我的朋友,你需要修改你的代码来处理多个上传。 我在这里看到的只是单个文件上传正在执行。相反,可以有类似这样的代码..(假设您为所有正在上传的文件保留相同的名称[upl_files])

$this->load->library('upload');

$number_of_files_uploaded = count($_FILES['upl_files']['name']);

for ($i = 0; $i < $number_of_files_uploaded; $i++) :
      $_FILES['userfile']['name']     = $_FILES['upl_files']['name'][$i];
      $_FILES['userfile']['type']     = $_FILES['upl_files']['type'][$i];
      $_FILES['userfile']['tmp_name'] = $_FILES['upl_files']['tmp_name'][$i];
      $_FILES['userfile']['error']    = $_FILES['upl_files']['error'][$i];
      $_FILES['userfile']['size']     = $_FILES['upl_files']['size'][$i];
      $config = array(
        'file_name'     => <your ouw function to generate random names>,
        'allowed_types' => 'jpg|jpeg|png|gif',
        'max_size'      => 3000,
        'overwrite'     => FALSE,

        /* real path to upload folder ALWAYS */
        'upload_path'
            => $_SERVER['DOCUMENT_ROOT'] . '/path/to/upload/folder'
      );
      $this->upload->initialize($config);
      if ( ! $this->upload->do_upload()) :
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
      else :
        $final_files_data[] = $this->upload->data();
        // Continue processing the uploaded data
      endif;
    endfor;
  }

现在有了这个$ final_files_data - 你可以进行进一步的处理。