在CodeIgniter中使用数组名称上载多个文件

时间:2013-09-19 00:07:15

标签: php arrays codeigniter

我的代码问题在这里。

案例是,我有多个输入文件格式,我需要获取当前数组的名称(包含数据库ID)并从中上传文件。

这是我的观点

foreach($result2 as $result3)
{
   print "
   <tr>
       <td><input type=file name=baddismantling[".$result3['iddeployment']."] size=20></td>
   </tr>
   ";
}

所以,也会有各种上传形式和名称,但它总是独一无二的。

在用户点击提交按钮后,该表单将在我的控制器中调用函数,这里是 处理它的功能

private function _ready_to_start($sessiondata)
{
    if($post['doSubmitDismantlingAction'] == 'uploadbad')
    {
        while ($fruit_name = current($_FILES['baddismantling']['name']))
        {
            if($fruit_name != false)
        {
                //GET ID FROM DEPLOYMENT DB
            $deploymentid = key($_FILES['baddismantling']['name']);
                $file = "baddismantling[$deploymentid]";
            if(!$this->upload->do_upload($file))
                {
                    $check3[] = $this->upload->display_errors()." target: ".$file;
                }
            $check2 = $this->upload->data();
        }
            next($_FILES['baddismantling']['name']);
        }
        //SHOW RESULT WHILE UPLOADING
        print implode($check3);
    }
}

如你所见,我成功获得了

  

$ deploymentid = 1

我希望...但是,浏览器给我:

  

您没有选择要上传的文件。 target:baddismantling [1]

任何人都可以就此问题向我提出建议?或者我的代码有问题?

注意:

  

配置没有问题$ this-&gt; upload-&gt;因为我   仅使用一个上传表单成功

1 个答案:

答案 0 :(得分:1)

感谢您抽出时间回答我的问题,我很幸运地通过以下代码解决了上述问题:

$check3 = array();
$count = 0;
$check = ($_FILES == true ? count($_FILES['baddismantling']['name']) : (0));
while ($fruit_name = ($check == true && $check > 0 ? current($_FILES['baddismantling']['name']) : (false)) || $count <= $check){
$count++;
if($fruit_name == true && key($_FILES['baddismantling']['name']) == true)
{
    $iddeployment = key($_FILES['baddismantling']['name']);
    /*THIS PART WAS HELPING ME*/if($_FILES['baddismantling']['name'][$iddeployment] == true)
    {
    $_FILES['userfile']['name']     = $_FILES['baddismantling']['name'][$iddeployment];
    $_FILES['userfile']['type']     = $_FILES['baddismantling']['type'][$iddeployment];
    $_FILES['userfile']['tmp_name'] = $_FILES['baddismantling']['tmp_name'][$iddeployment];
    $_FILES['userfile']['error']    = $_FILES['baddismantling']['error'][$iddeployment];
    $_FILES['userfile']['size']     = $_FILES['baddismantling']['size'][$iddeployment];
    if (!$this->upload->do_upload())
    {
        $check3[] = $this->upload->display_errors();
    }
    else
    {
        $uploaddata = $this->upload->data();
        $result['badfilename'] = $uploaddata['full_path'];
        $result['id'] = $iddeployment;
        $sql = $this->sql->updatedismantlingBAD($result);
    }
}
}
next($_FILES['baddismantling']['name']);}