如何在控制器中获取用户文件的名称以添加到数据库
How to get the name of userfiles in controller to add to database
这是到目前为止的代码。
我有 2 张图片正在上传并添加到所需的地图中。
我的问题是,如何从图像中获取两个名称,以便我可以将名称与其他表单信息一起添加到数据库中。
public 函数 CreateTypesForGamma() {
$type = new stdClass();
$type->TypeID = $this->input->post('typeID') == 0 ? null : $this->input->post('typeID');
$type->Titel = $this->input->post('titel');
$type->TechnischeFiche = $this->input->post('technischeFiche');
$type->GammaID = $this->input->post('gammaID');
$type->ExtLink = $this->input->post('extLink');
$type->InfoNL = $this->input->post('infoNL');
$type->InfoFR = $this->input->post('infoFR');
$type->InfoEN = $this->input->post('infoEN');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
if ($i == 0)
$images[] = $fileName;
}
$fileName = implode(',',$images);
$this->gamma_model->upload_image($fileName);
system.out.println($images);
if ($type->TypeID == 0) {
$type->TypeID = $this->type_model->insert($type);
} else {
$this->type_model->update($type);
}
redirect('gamma/viewAdminGamma');
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5120';
$config['overwrite'] = FALSE;
return $config;
}
将代码更改为以下
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
$images[] = $fileName;
$type->TypeFoto=$fileName[0];// this gives name of first
image
$type->Lastentabel =$fileName[1];
}
$fileName = implode(',',$images);
$this->gamma_model->upload_image($fileName);
system.out.println($images);
if ($type->TypeID == 0) {
$type->TypeID = $this->type_model->insert($type);
} else {
$this->type_model->update($type);
}
redirect('gamma/viewAdminGamma');
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5120';
$config['overwrite'] = FALSE;
return $config;
}
这是到目前为止的代码。 我有 2 张图片正在上传并添加到所需的地图中。 我的问题是,如何从图像中获取两个名称,以便我可以将名称与其他表单信息一起添加到数据库中。
public 函数 CreateTypesForGamma() {
$type = new stdClass();
$type->TypeID = $this->input->post('typeID') == 0 ? null : $this->input->post('typeID');
$type->Titel = $this->input->post('titel');
$type->TechnischeFiche = $this->input->post('technischeFiche');
$type->GammaID = $this->input->post('gammaID');
$type->ExtLink = $this->input->post('extLink');
$type->InfoNL = $this->input->post('infoNL');
$type->InfoFR = $this->input->post('infoFR');
$type->InfoEN = $this->input->post('infoEN');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
if ($i == 0)
$images[] = $fileName;
}
$fileName = implode(',',$images);
$this->gamma_model->upload_image($fileName);
system.out.println($images);
if ($type->TypeID == 0) {
$type->TypeID = $this->type_model->insert($type);
} else {
$this->type_model->update($type);
}
redirect('gamma/viewAdminGamma');
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5120';
$config['overwrite'] = FALSE;
return $config;
}
将代码更改为以下
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['userfile']['name'];
$images[] = $fileName;
$type->TypeFoto=$fileName[0];// this gives name of first
image
$type->Lastentabel =$fileName[1];
}
$fileName = implode(',',$images);
$this->gamma_model->upload_image($fileName);
system.out.println($images);
if ($type->TypeID == 0) {
$type->TypeID = $this->type_model->insert($type);
} else {
$this->type_model->update($type);
}
redirect('gamma/viewAdminGamma');
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = APPPATH . 'images/types'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5120';
$config['overwrite'] = FALSE;
return $config;
}