在CodeIgniter docs here
中该部分底部的注释说明:
您还可以处理传递给回调的表单数据并将其返回。如果你的回调返回布尔值以外的任何东西,则假定数据是你新处理的表格数据。
我有一个回调函数可以验证图片上传并进行上传,如下所示:
public function upload() {
$this->form_validation->set_rules('imageFile', 'Image', 'callback_validate_image');
// ...
}
然后是回调函数:
public function validate_image() {
if (empty($_FILES['imageFile'])) {
$this->form_validation->set_message('validate_image', 'Please select a file');
return false;
}
$config = array (
'upload_path' => './uploads/',
'allowed_types' => 'jpg|jpeg|png|gif',
'max_size' => '5048576'
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('imageFile')) {
$this->form_validation->set_message('validate_image', $this->upload->display_errors());
return false;
} else {
$data = array('upload_data' => $this->upload->data());
return $data;
}
}
如何将$data
返回到upload
函数,以便我可以在一个查询中将所有数据插入数据库?
答案 0 :(得分:1)
如果我做了你的代码部分,我会这样做
function upload()
{
// 'required' doesn't work on file inputs so use empty()
if (empty($_FILES['imageFile'])) {
// show error message
}
if ($this->form_validation->run() == FALSE)
{
echo "Please select a file";
//$this->load->view('myform');
}
else
{
$config = array (
'upload_path' => './uploads/',
'allowed_types' => 'jpg|jpeg|png|gif',
'max_size' => '5048576'
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('imageFile'))
{
$this->form_validation->set_message('validate_image', $this->upload->display_errors());
return false;
}
else {
$data['upload_data'] = $this->upload->data();
$data['form_data'] = $this->input->post(NULL, TRUE);
var_dump($data);
}
}
}
没有通过上传部分。在运行之前测试它