【发布时间】:2018-02-16 18:15:07
【问题描述】:
我有一个控制器方法来检查发布数据并通过 form_validation 运行方法对其进行解析。在我的视图中还有一个图片上传字段,可以上传图片。 问题是当上传的文件太大时,比如说超过 10MB,表单验证将返回我的验证规则中定义的每一个错误。 如果我上传的文件仍然太大,但不是太多,我会得到正确的错误信息。
我的代码如下所示:
public function addVacancy()
{
$this->is_logged_in();
//Check if user has correct permission to access this page
$aclConfig = array('userID' => $this->auth_user_id);
$this->load->library('acl', $aclConfig);
$redirect_protocol = USE_SSL ? 'https' : NULL;
if (!empty($this->auth_user_id)) {
if ( $this->acl->hasPermission("create_vacancy") ) {
//If the value is filled in we know it is an admin creating a vacancy for an organisation, we also set a boolean so we show a bit different message in confirmation.php
$orgId = $this->input->post('orgid');
if (empty($orgId)) {
$orgId = $this->userRoles_model->getOrgUserId($this->auth_user_id);
} else {
$adminEditing = TRUE;
}
$vacancyId = null;
$data = null;
if (strlen($this->input->post('occupancy')) != 1) {
$occupancy = 3;
} else {
$occupancy = $this->input->post('occupancy');
}
$vacancy_data = [
'org_id' => $orgId,
'offer' => $this->input->post('offer'),
'name' => $this->input->post('title'),
'website' => $this->input->post('website'),
'description' => $this->input->post('vacdescription'),
'address_line_1' => $this->input->post('route'),
'address_line_2' => $this->input->post('street_number'),
'address_postal_code' => $this->input->post('postalcode'),
'address_city' => $this->input->post('city'),
'address_country' => $this->input->post('country'),
'number_required' => $this->input->post('count'),
'engagement' => $this->input->post('time'),
'occupancy_kind' => $occupancy,
'create_time' => date('Y-m-d H:i:s'),
'accessibility' => $this->input->post('vacaccessibility'),
'status' => 0
];
$dates_data = [
'fullvacancydate' => $this->input->post('daterange-vacancy')
];
if (!empty($dates_data['fullvacancydate'])) {
$splitfromandto = explode(" - ", $dates_data['fullvacancydate']);
$vacancy_data['vacancy_start_date'] = date( 'Y-m-d', strtotime($splitfromandto[0]));
$vacancy_data['vacancy_end_date'] = date( 'Y-m-d', strtotime($splitfromandto[1]));
} else {
$today = date('Y-m-d');
$todayPlus6Months = date('Y-m-d', strtotime("+6 months", strtotime($today)));
$vacancy_data['vacancy_start_date'] = $today;
$vacancy_data['vacancy_end_date'] = $todayPlus6Months;
}
$contactId = $this->input->post('contactpersons');
if ($contactId == 0) {
$contact_data = [
'org_id' => $orgId,
'family_name' => $this->input->post('family_name'),
'first_name' => $this->input->post('first_name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'function' => $this->input->post('function')
];
}else{
$contact_data = (array) $this->orgContacts_model->get($contactId);
}
$this->load->library('form_validation');
$validation_data = array_merge($vacancy_data, $contact_data);
$this->form_validation->set_data($validation_data);
$validation_rules = [
[
'field' => 'offer',
'label' => 'offer',
'rules' => 'trim|required',
'errors' => ['required' => 'Wat heb je te bieden is verplicht.']
],
[
'field' => 'name',
'label' => 'name',
'rules' => 'trim|required',
'errors' => ['required' => 'Naam is verplicht.']
],
[
'field' => 'description',
'label' => 'description',
'rules' => 'trim|required',
'errors' => ['required' => 'Beschrijving is verplicht.']
],
[
'field' => 'address_postal_code',
'label' => 'address_postal_code',
'rules' => 'trim|required',
'errors' => ['required' => 'Postcode is verplicht.']
],
[
'field' => 'address_city',
'label' => 'address_city',
'rules' => 'trim|required',
'errors' => ['required' => 'Stad of gemeente is verplicht.']
],
[
'field' => 'address_country',
'label' => 'address_country',
'rules' => 'trim|required',
'errors' => ['required' => 'Land is verplicht.']
],
[
'field' => 'number_required',
'label' => 'number_required',
'rules' => 'trim|required',
'errors' => ['required' => 'Aantal vrijwilligers is verplicht.']
],
[
'field' => 'occupancy_kind',
'label' => 'occupancy_kind',
'rules' => 'trim|required',
'errors' => ['required' => 'Bezetting is verplicht.']
],
[
'field' => 'family_name',
'label' => 'family_name',
'rules' => 'trim|required',
'errors' => ['required' => 'Familie naam van de contactpersoon is verplicht.']
],
[
'field' => 'first_name',
'label' => 'first_name',
'rules' => 'trim|required',
'errors' => ['required' => 'Voornaam van de contactpersoon is verplicht.']
],
[
'field' => 'email',
'label' => 'email',
'rules' => 'trim|required',
'errors' => ['required' => 'Email van de contactpersoon is verplicht.']
],
[
'field' => 'phone',
'label' => 'phone',
'rules' => 'trim|required',
'errors' => ['required' => 'Telefoonnummer van de contactpersoon is verplicht.']
],
[
'field' => 'function',
'label' => 'function',
'rules' => 'trim|required',
'errors' => ['required' => 'Functie van de contactpersoon is verplicht.']
],
];
$this->form_validation->set_rules($validation_rules);
if ($this->form_validation->run()) {
// Create new contact
if ($contactId == 0) {
$contactId = $this->orgContacts_model->add($contact_data);
}
// Create vacancy
$this->load->model('vacancy/vacancy_model');
// Var to check
$check = false;
$isNoImageChosen = false;
if(isset($_FILES['userfile']['name']) && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
// Banner Upload
$config['upload_path'] = VACANCY_IMAGES;
$config['allowed_types'] = 'jpg|png|PNG|JPG|jpeg|bmp';
$config['min_width'] = 1024;
$config['min_height'] = 768;
$config['max_size'] = 3072;
$config['quality'] = 60;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) {
$uploadData = $this->upload->data();
if (isset($uploadData)) {
$vacancy_data['banner'] = $uploadData['file_name'];
$check = true;
}
} else {
$imageOk = array('error' => $this->upload->display_errors());
// Passing Variables
$data['title'] = 'Give a Day - Error';
$data['class'] = 'vacancy';
$data['validationErrors'] = validation_errors();
$data['imgErrors'] = $imageOk['error'];
$data['feedback'] = array(
'type' => 'alert-danger',
'icon' => 'fa-times',
'title' => 'Oops!',
'text' => 'Uploaden van de foto is niet gelukt'
);
$content = 'dashboard/vacancy/error';
}
} else {
//check on true cause there is no image selected
$check = true;
$isNoImageChosen = true;
}
//Check if image upload went without errors
if($check) {
//Serialize the accessibility (because more than 1 value can be entered)
$vacancy_data['accessibility'] = serialize($vacancy_data['accessibility']);
$vacancyId = $this->vacancy_model->add($vacancy_data);
// Create Vacancy Contact
$vacancyContact['contact_id'] = $contactId;
$vacancyContact['vacancy_id'] = $vacancyId;
$this->vacancyContacts_model->add($vacancyContact);
// Set Interests
$this->load->model('vacancy/vacancyInterests_model');
$interests = $this->input->post('interests');
if ($interests != '') {
$interests = explode(",", $interests);
foreach ($interests as $id) {
$this->vacancyInterests_model->add($vacancyId, $id);
}
}
if ($isNoImageChosen) {
$k = array_rand($interests);
$randomInterestId = $interests[$k];
$randomBannerImageLocation = return_random_interest_banner($randomInterestId);
//Because the vacancy has already been created we need to do an update for setting the banner image
$bannerData = array (
'banner' => $randomBannerImageLocation
);
$this->vacancies_model->update($vacancyId, $bannerData);
}
// Set Skills
$this->load->model('vacancy/vacancySkills_model');
$skills = $this->input->post('skills');
if ($skills != '') {
$skills = explode(",", $skills);
foreach ($skills as $id) {
$this->vacancySkills_model->add($vacancyId, $id);
}
}
//Get organisation information for the name
$orgdata = $this->organization_model->get($orgId);
$orgname = $orgdata->name;
$orguserid = $this->userRoles_model->getAdminUserId($orgId);
$orguserdata = $this->users_model->get($orguserid[0]->user_id);
$orgemail = $orguserdata->email;
$mail_data['vacid'] = $vacancyId;
$mail_data['vacname'] = $vacancy_data['name'];
$mail_data['org_id'] = $orgId;
$mail_data['orgname'] = $orgname;
$mail_data['contactpersonfirstname'] = $contact_data['first_name'];
$this->email->from(GENERAL_MAIL, 'Give a Day');
$this->email->to($contact_data['email']);
$this->email->cc($orgemail);
$this->email->bcc(GENERAL_MAIL);
$this->email->subject('Je vrijwilligersactiviteit is aangemaakt');
$message = $this->load->view('mail/vacancy/creation_confirmation', $mail_data, TRUE);
$this->email->message($message);
$this->email->send();
// Passing Variables
$data['title'] = 'Give a Day - Succes!';
$data['class'] = 'vacancy';
$data['vacancyId'] = $vacancyId;
$data['vacancyName'] = $vacancy_data['name'];
if ($adminEditing) {
$data['adminedit'] = TRUE;
$data['orgid'] = $orgId;
}
$content = 'dashboard/vacancy/confirmation';
} else {
// Passing Variables
$data['title'] = 'Give a Day - Error';
$data['class'] = 'vacancy';
$data['validationErrors'] = validation_errors();
$imageOk = array('error' => $this->upload->display_errors());
$data['imgErrors'] = $imageOk['error'];
$content = 'dashboard/vacancy/error';
}
} else {
// Passing Variables
$data['title'] = 'Give a Day - Error';
$data['class'] = 'vacancy';
$data['validationErrors'] = validation_errors();
//$imageOk = array('error' => $this->upload->display_errors());
//$data['imgErrors'] = $imageOk['error'];
$content = 'dashboard/vacancy/error';
}
// Template declaration
$partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation_dashboard', 'content' => $content, 'footer' => '_master/footer/footer');
$this->template->load('_master/master', $partials, $data);
} else {
//User does not have correct permissions --> Go to 404
redirect(site_url( 'error/my404', $redirect_protocol));
}
} else {
//User is anonymous -> go to login page (and remember the current URL for redirecting again after log in)
$this->session->set_flashdata('referred_from', uri_string());
redirect(site_url( 'login?redirect=user', $redirect_protocol));
}
}
请注意,当我上传 19 MB 的图像时,由于某种原因,我最终会一直处于底部的 else 子句中,并且我得到一个显示所有验证错误的页面,上面写着“Wat heb je te bieden is verplicht. ", "naam is verplicht", ... 所有这些都是字面意思。 所有数据均已正确填写,当我执行完全相同的步骤,但拍摄小于 3MB 的图像时,它可以完美运行。
所以 0-3MB - 完美。 3-10MB - 给出完美的错误 和 >10 MB - 给出所有验证错误。
【问题讨论】:
-
你使用的是哪个 CI 和 PHP 版本?
-
要筛选的代码很多。您应该将向我们展示的代码减少到与问题相关的代码。
-
^ 请将代码缩小到仅相关的内容。也许在这样做的过程中,您还可以通过消除过程找到问题。当涉及到这么多代码时,我们几乎不可能复制并因此修复问题,除非错误本质上是印刷错误。
-
无关:
$config['quality'] = 60;不是上传库中的选项。如果您想调整大小和更改质量,建议使用 image_lib,如果您只想更改质量 image_lib 将不起作用(在另一个问题中有它)使用类似 verot upload 的东西。 -
为了减小图像大小,我更喜欢使用 html5 画布 developer.mozilla.org/en-US/docs/Web/API/Canvas_API 进行客户端图像处理,而不是 Php 图像库(由 CI 使用)。至少我的服务器不会为超大图片上传付费。回到 OP:在我看来,服务器不处理更大的图像并抛出 500,因此您会遇到
"I end up in the else clause all the way at the bottom "
标签: forms codeigniter validation