How to upload a file in Drupal 7 using forms?

I am having problems with Drupal 7 and downloading files.

My code that does not work:

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).'-');

}

So, the path is created, but the file does not load, and file_save_upload returns FALSE. Also I tried array () and true as $ validators with no effect.

Any help is greatly appreciated. Thank.

+3
source share
1 answer

Doh. $ destination should not contain a file name, only the path.

+4
source

Source: https://habr.com/ru/post/1775976/


All Articles