Drupal 7 保留文件上传 [英] Drupal 7 retain file upload
问题描述
我有一个文件上传表单
如何在出现其他验证错误时保留此文件,以便用户不必再次上传文件?
how can I retain this file when there are other validation errors so that the user doesn't have to upload the file again?
我在我的验证函数中尝试了这个,但它不起作用:
I tried this in my validation function but it doesn't work:
function mymodule_someform_validate($form, &$form_state) {
$form_state["values"]["some_field"] = some_value;
}
$form_state["values"] 变量在我的表单定义函数中不可用 - mymodule_someform($form, &$form_state)
the $form_state["values"] variable is not available in my form definition function - mymodule_someform($form, &$form_state)
有什么想法吗?
推荐答案
只需使用 managed_file
类型,它就会为您完成:
Just use the managed_file
type, it'll do it for you:
$form['my_file_field'] = array(
'#type' => 'managed_file',
'#title' => 'File',
'#upload_location' => 'public://my-folder/'
);
然后在您的提交处理程序中:
And then in your submit handler:
// Load the file via file.fid.
$file = file_load($form_state['values']['my_file_field']);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
如果验证失败并且用户离开表单,该文件将在几个小时后自动删除(因为 file_managed
表中没有 FILE_STATUS_PERMANENT
的所有文件都是).如果验证没有失败,提交处理程序将运行,文件将在系统中被标记为永久.
If the validation fails and the user leaves the form, the file will be automatically deleted a few hours later (as all files in the file_managed
table without FILE_STATUS_PERMANENT
are). If the validation doesn't fail, the submit handler will be run and the file will be marked as permanent in the system.
这篇关于Drupal 7 保留文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!