我想用php创建具有多个部分和数据类型的表单。如: 输入文本(名称,标题,描述等) 输入图像(header_img) 输入多个图像(滑块)
关键是我要创建大量数据包括使用php在一个页面中上传文件。 在post方法中,我尝试使用标准逻辑,使用if else逻辑逐个上传图像,使用大量if else逻辑和不同名称并将其插入数据库的每个字段中(一个字段合二为一)现场数据库)huft ....
if($_FILES['imghead']['size']!=0){
// run upload method
}
对于编辑,我手动尝试,与post方法相同,为每个字段使用if else。例如:
if($_FILES['imghead']['size']!=0){
// run upload method
}
if($_FILES['slider1']['size']!=0){
// run upload method
}
if($_FILES['slider2']['size']!=0){
// run upload method
}
if($_FILES['slider3']['size']!=0){
// run upload method
}
// etc
但问题是,我认为我的方法不是很好,它没有效果,我必须编写很多代码并用if else逻辑检查它们。
我的问题: 你知道如何优化我的逻辑到真正简单的代码吗?特别是CRUD。
我尝试过杂货,但我需要更多功能,如多张图片上传
答案 0 :(得分:1)
有一种方法可以使用grocerycrud实现多个图像上传,在控制器中你需要为你需要的每个上传字段设置一个不同的名称方法,然后只需将主方法粘贴到Grocery_CRUD.php文件中并更改名称与您指定的名称相同
控制器代码:
$crud->set_field_upload('file1','assets/uploads');
$crud->set_another_field_upload('file2','assets/uploads');
这是您需要复制的代码,(route:htdocs \ app_name \ application \ libraries \ Grocery_CRUD.php)
public function set_field_upload($field_name, $upload_dir = '')
{
$upload_dir = !empty($upload_dir) && substr($upload_dir,-1,1) == '/'
? substr($upload_dir,0,-1)
: $upload_dir;
$upload_dir = !empty($upload_dir) ? $upload_dir : 'assets/uploads/files';
/** Check if the upload Url folder exists. If not then throw an exception **/
if (!is_dir(FCPATH.$upload_dir)) {
throw new Exception("It seems that the folder \"".FCPATH.$upload_dir."\" for the field name
\"".$field_name."\" doesn't exists. Please create the folder and try again.");
}
$this->upload_fields[$field_name] = (object) array(
'field_name' => $field_name,
'upload_path' => $upload_dir,
'encrypted_field_name' => $this->_unique_field_name($field_name));
return $this;
}
只需将其粘贴到下方并使用控制器中指定的相同名称更改功能名称:
public function set_another_field_upload($field_name, $upload_dir = '')
{
$upload_dir = !empty($upload_dir) && substr($upload_dir,-1,1) == '/'
? substr($upload_dir,0,-1)
: $upload_dir;
$upload_dir = !empty($upload_dir) ? $upload_dir : 'assets/uploads/files';
/** Check if the upload Url folder exists. If not then throw an exception **/
if (!is_dir(FCPATH.$upload_dir)) {
throw new Exception("It seems that the folder \"".FCPATH.$upload_dir."\" for the field name
\"".$field_name."\" doesn't exists. Please create the folder and try again.");
}
$this->upload_fields[$field_name] = (object) array(
'field_name' => $field_name,
'upload_path' => $upload_dir,
'encrypted_field_name' => $this->_unique_field_name($field_name));
return $this;
}