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
类型即可,您可以:
$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_STATUS_PERMANENT
的 file_managed
表中的所有文件都是)。如果验证没有失败,则提交处理程序将被运行,文件将被标记为系统中的永久性。
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屋!