我正在使用 Drupal 7 并有一个自定义模块,该模块创建一个具有文件上传字段的表单:
$form['resume_file'] = array(
'#type' => 'file',
'#title' => t('Resume Upload'),
);
我需要确保文件扩展名是以下之一:doc、docx、pdf、txt 和 rtf,并且文件大小不大于 2MB
我在文档中没有找到明确的方法来实现这一点。我看到一个地方说使用这个:
$form['resume_file'] = array(
'#type' => 'file',
'#title' => t('Resume Upload'),
'#upload_validators' => array("file_validate_extensions" => array("doc docx pdf txt rtf")),
);
但这并没有阻止错误的文件类型并给出错误消息。我是否需要做其他事情,比如在我的 hook_form_validate() 函数中添加一些额外的东西?
我也看到了这个:
$form['resume_file'] = array(
'#type' => 'file',
'#title' => t('Resume Upload'),
);
$form['resume_file']['#upload_validators']['file_validate_extensions'][0] = 'doc docx pdf txt rtf';
其中也没有做任何事情。如何验证文件大小和扩展名?