如何使用表单在Drupal 7中上传文件?

时间:2010-11-20 14:19:09

标签: drupal file-upload drupal-7

我在使用Drupal 7和文件上传方面遇到了麻烦。

我的代码不起作用:

function test_form($form, &$form_state){

$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['podcast'] = array(
    '#title' => 'Audio file',
    '#type' => 'file',
);
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
);
return $form;

}

function test_form_submit($form, &$form_state){

$vals = $form_state['values'];
$filepath = 'public://test/';
//$filepath = 'temporary://test/';
$filename = 'rcc_date.mp3';

file_prepare_directory($filepath, FILE_CREATE_DIRECTORY);
$file = file_save_upload('podcast', array('file_validate_extensions' => array()), $filepath.$filename);
//got FALSE here. Why?
die(print_r($file===FALSE).'-');

}

因此创建了路径,但文件没有上传,file_save_upload返回FALSE。我也尝试使用array()和true作为$ validators而没有效果。

任何帮助非常感谢。感谢。

1 个答案:

答案 0 :(得分:4)

卫生署。 $ destination不应包含filename,只包含path。