我有一個上傳表單,允許我根據需要添加儘可能多的文件。但是,當我開始嘗試上傳文件時,出現錯誤。Codeigniter,上傳同名多個文件
控制器
$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'))
它的工作原理,但只有一個文件,它似乎沒有循環輪再次不某些原因
什麼是D **錯誤**你正在得到? –
該文件沒有上傳到任何文件夾,'echo json_encode(array('status'=> $ status,'msg'=> $ msg));'顯示'{「status」:「error」,「msg」:「保存文件時出錯,請重試。」}'。所以這個錯誤來源於配置初始化之後。任何想法? – Pooshonk
似乎你的文件正在上傳,但你從哪裏設置'$ data ['raw_name']'的值? –