首页 > 解决方案 > 多个文件上传控件在 CI 中不起作用

问题描述

在 CodeIgniter 中,我有 4 个文件上传控件,如下所示:

<?php echo form_open_multipart('uploadimg');?>
<input type="file" name="image1" />
<input type="file" name="image2" />
<input type="file" name="image3" />
<input type="file" name="image4" />
<input type="submit" value="upload" />
<?php echo form_close();?>

以下是我的文件上传代码:

$name1="img1.jpg";
$config1=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name1,
    );
$this->load->library('upload',$config1);
if($this->upload->do_upload('image1'))
{
    echo "Image 1 has been uploaded successfully";
}
else
{
    echo $this->upload->display_errors();
}
$name2="img2.jpg";
$config2=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name2,
    );
$this->load->library('upload',$config2);
if($this->upload->do_upload('image2'))
{
    echo "Image 2 has been uploaded successfully";
}
else
{
    echo $this->upload->display_errors();
}
$name3="img3.jpg";
$config3=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name3,
    );
$this->load->library('upload',$config3);
if($this->upload->do_upload('image3'))
{
    echo "Image 3 has been uploaded successfully";
}
else
{
    echo $this->upload->display_errors();
}
$name4="img4.jpg";
$config4=array(
    'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
    'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite'=>TRUE,
    'max_size'=>"500",
    'file_name'=>$name4,
    );
$this->load->library('upload',$config4);
if($this->upload->do_upload('image4'))
{
    echo "Image 4 has been uploaded successfully";
}
else
{
  echo $this->upload->display_errors();
}

我得到以下输出:


图1上传成功图2上传成功图3上传成功图4上传成功


但是,在上传文件夹中,我只得到 1 个文件。奇怪的是,正在显示的文件是最后一个文件上传控制,但文件的名称是img1.,即第一个上传文件的名称。

为什么会这样?解决办法是什么?

标签: phpcodeigniterfile-uploadcodeigniter-2codeigniter-3

解决方案


使用 initialize() 方法设置每个配置,而不是在加载上传库时设置。

改变:

$this->load->library('upload', $config*);

到:

$this->upload->initialize($config*);

更新代码:

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

$name1   = "img1.jpg";
$config1 = array(
    'upload_path' => FCPATH . "uploads/",
    'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite' => TRUE,
    'max_size' => "500",
    'file_name' => $name1
);

$this->upload->initialize($config1);
if ($this->upload->do_upload('image1')) {
    echo "Image 1 has been uploaded successfully";
} else {
    echo $this->upload->display_errors();
}
$name2   = "img2.jpg";
$config2 = array(
    'upload_path' => FCPATH . "uploads/",
    'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite' => TRUE,
    'max_size' => "500",
    'file_name' => $name2
);
$this->upload->initialize($config2);

if ($this->upload->do_upload('image2')) {
    echo "Image 2 has been uploaded successfully";
} else {
    echo $this->upload->display_errors();
}
$name3   = "img3.jpg";
$config3 = array(
    'upload_path' => FCPATH . "uploads/",
    'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite' => TRUE,
    'max_size' => "500",
    'file_name' => $name3
);
$this->upload->initialize($config3);

if ($this->upload->do_upload('image3')) {
    echo "Image 3 has been uploaded successfully";
} else {
    echo $this->upload->display_errors();
}
$name4   = "img4.jpg";
$config4 = array(
    'upload_path' => FCPATH . "uploads/",
    'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
    'overwrite' => TRUE,
    'max_size' => "500",
    'file_name' => $name4
);
$this->upload->initialize($config4);
if ($this->upload->do_upload('image4')) {
    echo "Image 4 has been uploaded successfully";
} else {
    echo $this->upload->display_errors();
}

推荐阅读