0

我有一个上传表单,允许我根据需要添加任意数量的文件。但是,当我开始尝试上传文件时,出现错误。

控制器

$this->load->library('upload'); 
$error = "";
$file  = "";
$this->total_count_of_files = count($_FILES['user_certificates']['name']);
print_r($_FILES['user_certificates']['name']);

for($i=0; $i<$this->total_count_of_files; $i++)
{
    $_FILES['user_certificates']['name'] = $_FILES['user_certificates']['name'][$i];
    $_FILES['user_certificates']['type'] = $_FILES['user_certificates']['type'][$i];
    $_FILES['user_certificates']['tmp_name'] = $_FILES['user_certificates']['tmp_name'][$i];
    $_FILES['user_certificates']['error'] = $_FILES['user_certificates']['error'][$i];
    $_FILES['user_certificates']['size'] = $_FILES['user_certificates']['size'][$i];

    $config['encrypt_name'] = TRUE;
    $config['file_name'] = $_FILES['user_certificates']['name'];
    $config['upload_path'] = './certificate_files/';
    $config['allowed_types'] = 'txt|pdf|doc|docx';
    $config['max_size'] = 0;

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

    if (!$this->upload->do_upload())
    {
        $status = 'error';
        $msg = $this->upload->display_errors();
    }
    else
    {
        $data = $this->upload->data();
        $file = $data['raw_name'] . $data['file_ext'];
    }
    if($file)
    {
        $status = "success";
        $msg = "Image successfully uploaded";
    }
    else
    {
        $status = "error";
        $msg = "Something went wrong when saving the image, please try again.";
    }
}   
echo json_encode(array('status' => $status, 'msg' => $msg));
exit();

print_r($_FILES['user_certificates']['name']))我展示了我添加的文件:

Array ( [0] => license.txt [1] => license.txt )

我完全坚持如何让上传工作。有任何想法吗?

提前干杯!

编辑

如果我改变这个:

if (!$this->upload->do_upload())

对此:

if (!$this->upload->do_upload('user_certificates'))

它有效,但仅适用于一个文件,由于某种原因它似乎不再循环

4

1 回答 1

2

您的循环似乎不正确。

...
$_FILES['user_certificates']['name'] = $_FILES['user_certificates']['name'][$i];
...

这些行覆盖了原始的 $_FILES 数组,因此在完成第一个循环后,它不会在循环中找到任何其他内容,因为它被覆盖了。

相反,您可以先将 $_FILES 数组存储到局部变量中,然后循环遍历它以逐个上传。

于 2013-05-16T11:33:29.903 回答