Created
January 22, 2011 03:59
-
-
Save brockboland/790842 to your computer and use it in GitHub Desktop.
A basic example of adding a file upload field to a Drupal form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_form_alter(). | |
*/ | |
function filetest_form_alter(&$form, &$form_state, $form_id) { | |
switch ($form_id) { | |
case 'contact_mail_page': | |
// Need to set the enctype so the form will allow file uploads | |
$form['#attributes']['enctype'] = "multipart/form-data"; | |
$form['new_file'] = array( | |
'#type' => 'file', | |
'#title' => t('New File'), | |
'#description' => t('Select the file you want to attach.'), | |
); | |
$form['#submit'][] = 'filetest_submit'; | |
break; | |
} | |
} | |
function filetest_submit($form, &$form_state) { | |
$file = file_save_upload('new_file', array(), file_directory_path()); | |
// Print data so we can see what we've got | |
//var_dump($file); | |
//die(); | |
// Some of the values available from the $file object: | |
$file_name = $file->filename; // "Letter.pdf" | |
$file_path = $file->filepath; // "sites/default/files/Letter.pdf" | |
$file_mime = $file->filemime; // "application/pdf" | |
$file_size = $file->filesize; // "28711" | |
$file_timestamp = $file->timestamp; // 1295668507 | |
$fid = $file->fid; // "15" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment