function _webform_validate_file in Webform 6.2
Same name and namespace in other branches
- 5.2 components/file.inc \_webform_validate_file()
- 5 components/file.inc \_webform_validate_file()
- 6.3 components/file.inc \_webform_validate_file()
1 string reference to '_webform_validate_file'
- _webform_render_file in components/
file.inc - Build a form item array containing all the properties of this component.
File
- components/
file.inc, line 311 - Webform module file component.
Code
function _webform_validate_file($form_element, &$form_state) {
$component = $form_element['#webform_component'];
$form_key = $component['form_key'];
if (empty($_FILES['files']['name'][$form_key])) {
return;
}
// Build a human readable list of extensions:
$extensions = $component['extra']['filtering']['types'];
$extension_list = '';
if (count($extensions) > 1) {
for ($n = 0; $n < count($extensions) - 1; $n++) {
$extension_list .= $extensions[$n] . ', ';
}
$extension_list .= 'or ' . $extensions[count($extensions) - 1];
}
else {
$extension_list = $extensions[0];
}
if (in_array('jpg', $extensions)) {
$extensions[] = 'jpeg';
}
$dot = strrpos($_FILES['files']['name'][$form_key], '.');
$extension = drupal_strtolower(substr($_FILES['files']['name'][$form_key], $dot + 1));
if (!in_array($extension, $extensions)) {
form_set_error($form_key, t("Files with the '%ext' extension are not allowed, please upload a file with a %exts extension.", array(
'%ext' => $extension,
'%exts' => $extension_list,
)));
}
// Now let's check the file size (limit is set in KB).
if ($_FILES['files']['size'][$form_key] > $component['extra']['filtering']['size'] * 1024) {
form_set_error($form_key, t("The file '%filename' is too large (%filesize KB). Please upload a file %maxsize KB or smaller.", array(
'%filename' => $_FILES['files']['name'][$form_key],
'%filesize' => (int) ($_FILES['files']['size'][$form_key] / 1024),
'%maxsize' => $component['extra']['filtering']['size'],
)));
}
}