我有一个带有一些文本输入字段和三个文件上传字段的html表单。其中两个应该是图像,一个应该是文件(pdf)。我正在使用jquery ajax提交此表单。我的代码运行良好,但我觉得这不是正确的OO方式,我觉得我在重复我的自我。所以,如果有人能够以适当的方式指导我,那将是一个很大的帮助。
这是我的HTML表单
<form class="form-horizontal save-image-form" enctype="multipart/form-data" method="POST" action="<?php echo base_url() . 'loc_emp_reg_prc'; ?>">
<fieldset>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="loc_emp_idCpy">Employee ID Copy</label>
<div class="col-md-4">
<input id="loc_emp_idCpy" name="loc_emp_idCpy" class="input-file" type="file">
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="loc_emp_cntrct">Employee Contract</label>
<div class="col-md-4">
<input id="loc_emp_cntrct" name="loc_emp_cntrct" class="input-file" type="file">
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="loc_emp_pht">Employee Photo</label>
<div class="col-md-4">
<input id="loc_emp_pht" name="loc_emp_pht" class="input-file" type="file">
</div>
</div>
</fieldset>
</form>
这是控制器功能
function loc_emp_reg_prc() {
if ($this->form_validation->run('loc_emp_reg') == FALSE) {
$validation_errors = validation_errors();
echo ($validation_errors);
} else {
$salt = 'ophl';
$lstEmpNbr = $this->home_basic_curd->select_ordered_and_limited('loc_emp_id', 'DESC', 'lco_emp_nmbr', 1, 'loc_emp');
$newEmpNbr = $this->empNumberCreate($lstEmpNbr, $salt);
$postArray = $this->input->post();
$this->admin_image_manage->create_folder('uploads/' . $newEmpNbr);
$uploadPath = 'uploads/' . $newEmpNbr . '/';
$max_size = '3000';
$max_width = '400';
$max_height = '600';
$types1 = 'gif|jpg|png|JPG|GIF|PNG|PDF|pdf';
$types2 = 'PDF|pdf';
$errorString = "";
$is_upload = $this->admin_image_manage->upload_this_image($uploadPath, $max_size, $max_width, $max_height, 'loc_emp_idCpy', $types1);
if ($is_upload['is_uploaded']) {
$post_array['loc_emp_idCpy'] = $is_upload['rdata'];
echo $post_array['loc_emp_idCpy'];
} else {
foreach ($is_upload['rdata'] as $error) {
$errorString = $error;
}
}
if (!empty($_FILES['loc_emp_cntrct']['name'])) {
$is_upload = $this->admin_image_manage->upload_document($uploadPath, $max_size, 'loc_emp_cntrct', $types2);
if ($is_upload['is_uploaded']) {
$post_array['loc_emp_cntrct'] = $is_upload['rdata'];
echo $post_array['loc_emp_cntrct'];
} else {
foreach ($is_upload['rdata'] as $error) {
$errorString = $error;
}
}
}
$is_upload = $this->admin_image_manage->upload_this_image($uploadPath, $max_size, $max_width, $max_height, 'loc_emp_pht', $types1);
if ($is_upload['is_uploaded']) {
$post_array['loc_emp_pht'] = $is_upload['rdata'];
echo $post_array['loc_emp_pht'];
} else {
foreach ($is_upload['rdata'] as $error) {
$errorString = $error;
}
}
echo $errorString;
}
}
这是模特功能&#39; admin_image_manage&#39;
function upload_this_image($uploadPath, $max_size, $max_width, $max_height, $image, $types) {
$config = array(
'allowed_types' => $types, //only accept these file types
'upload_path' => $uploadPath, //upload directory
'remove_spaces' => TRUE,
'max_size' => $max_size,
'max_width' => $max_width,
'max_height' => $max_height,
);
$this->load->library('upload', $config);
$upload_errors = "";
if (!$this->upload->do_upload($image)) {
$error = array('error' => $this->upload->display_errors());
return array(
'is_uploaded' => 0,
'rdata' => $error
);
} else {
$image_data = $this->upload->data();
$image = $image_data['file_name'];
return array(
'is_uploaded' => 1,
'rdata' => $image
);
}
}
function create_folder($folderPath) {
if (is_dir($folderPath)) {
return FALSE;
} else {
mkdir($folderPath);
return TRUE;
}
}