Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Drupal 7保留文件上载_Drupal_File Upload_Drupal 7_Form Api - Fatal编程技术网

Drupal 7保留文件上载

Drupal 7保留文件上载,drupal,file-upload,drupal-7,form-api,Drupal,File Upload,Drupal 7,Form Api,我有一张文件上传表 当存在其他验证错误时,如何保留此文件,以便用户不必再次上载该文件 我在验证函数中尝试了此方法,但不起作用: function mymodule_someform_validate($form, &$form_state) { $form_state["values"]["some_field"] = some_value; } $form_state[“values”]变量在我的表单定义函数-mymodule_someform($form,&$form_stat

我有一张文件上传表

当存在其他验证错误时,如何保留此文件,以便用户不必再次上载该文件

我在验证函数中尝试了此方法,但不起作用:

function mymodule_someform_validate($form, &$form_state) {
  $form_state["values"]["some_field"] = some_value;
}
$form_state[“values”]变量在我的表单定义函数-mymodule_someform($form,&$form_state)中不可用


有什么想法吗?

只要使用
managed\u文件
类型,它就可以为您完成:

$form['my_file_field'] = array(
  '#type' => 'managed_file',
  '#title' => 'File',
  '#upload_location' => 'public://my-folder/'
);
然后在提交处理程序中:

// 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);

如果验证失败且用户离开表单,文件将在几个小时后自动删除(因为
文件管理
表中没有
文件状态_永久
的所有文件都被删除)。如果验证没有失败,提交处理程序将运行,文件将在系统中标记为永久文件。

只需使用
托管文件类型,它就可以为您完成:

$form['my_file_field'] = array(
  '#type' => 'managed_file',
  '#title' => 'File',
  '#upload_location' => 'public://my-folder/'
);
然后在提交处理程序中:

// 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);

如果验证失败且用户离开表单,文件将在几个小时后自动删除(因为
文件管理
表中没有
文件状态_永久
的所有文件都被删除)。如果验证没有失败,则将运行提交处理程序,并在系统中将文件标记为永久文件。

管理员表单示例供其他可能正在查看的人使用:

function example_admin_form(){  

  $form = array();  

  $form['image'] = array(
      '#type' => 'managed_file',
      '#name' => 'image',
      '#title' => t('upload your image here!'),
      '#default_value' => variable_get('image', ''),
      '#description' => t("Here you can upload an image"),
      '#progress_indicator' => 'bar',
      '#upload_location' => 'public://my_images/'
  );

  // Add your submit function to the #submit array
  $form['#submit'][] = 'example_admin_form_submit';

  return system_settings_form($form);
}

function example_admin_form_submit($form, &$form_state){

  // Load the file
  $file = file_load($form_state['values']['image']);

  // Change status to permanent.
  $file->status = FILE_STATUS_PERMANENT;

  // Save.
  file_save($file);

}

其他可能正在查看的管理员表单示例:

function example_admin_form(){  

  $form = array();  

  $form['image'] = array(
      '#type' => 'managed_file',
      '#name' => 'image',
      '#title' => t('upload your image here!'),
      '#default_value' => variable_get('image', ''),
      '#description' => t("Here you can upload an image"),
      '#progress_indicator' => 'bar',
      '#upload_location' => 'public://my_images/'
  );

  // Add your submit function to the #submit array
  $form['#submit'][] = 'example_admin_form_submit';

  return system_settings_form($form);
}

function example_admin_form_submit($form, &$form_state){

  // Load the file
  $file = file_load($form_state['values']['image']);

  // Change status to permanent.
  $file->status = FILE_STATUS_PERMANENT;

  // Save.
  file_save($file);

}

是的,我可能只会使用默认的节点形式。我想做一个自定义表单,因为我不想用它的ajax文件上传来使用节点的表单。但是我仍然不知道为什么传递给表单定义函数的表单状态没有设置“值”…是的,我可能只使用默认的节点表单。我想做一个自定义表单,因为我不想用它的ajax文件上传来使用节点的表单。但是我仍然不知道为什么传递给表单定义函数的表单状态没有设置“值”。。。