given a form element named 'upload' of type 'upload_element', i have a submit function that looks like this:

$dest = 'recruits';
  $fid = 0;
  if (is_object($form_state['values']['upload'])) {
    $fid = upload_element_save($form_state['values']['upload'], $dest, FILE_EXISTS_RENAME, FALSE, TRUE);
  }

it seems to work just fine. i am taking advantage of the built-in validation options for size and file type. should i also have a regular validation function for my form if this is the only element? i'm failing to think of anything else i should be checking. what is your recommendation here?

also, in my submit function, after the part i have quoted here, i will be running through the contents and processing each line. however, before i do that, is there anything else you think i should check? of course i will check to see if $fid == 0 before processing it.

i think perhaps another short example in the README would clear this up for users like me.

great module by the way. i had begun reading all of the standard drupal text for file handling when i came across this module. this is much easier to use.

Comments

alan d.’s picture

"should i also have a regular validation function for my form "

I can not think of anything other than "size and file type".

"however, before i do that, is there anything else you think i should check? of course i will check to see if $fid == 0 before processing it."

It's been a while, but "is_object($form_state['values']['upload'])" should only be an object if the file was uploaded, and 0 would then only be used if the existing file was deleted. To delete all you need to do is toggle this flag and cron will delete the file and the db record.

I guess just double check the {files} table to make sure that status is set to 1, otherwise the files get dumped during the next cron run :) But I'm sure one of the few thousand users would have issued a bug report by now if there were issues! But if you rewrite the handler, it is best to check.

Off topic, but remember that there is an issue with cached preview states. The cached form ID changes, and well it is effectively impossible to handle without hacking core or totally refactoring the module. Managed File works in core due to multiple core modifications, but uses effectively the same processing path. It will not be an issue with a single element form. ;)

Enjoy.

rconstantine’s picture

Status: Active » Closed (fixed)

Thanks. Got it sorted.

twiik’s picture

As I still struggled with setting the proper #default_value after this was closed I will add this for anyone else struggling with the same. It's probably the wrong way of approaching this, but whatever. It took me a while and it works. :)

Complete example of what's working for me:

function frontpage_admin_settings($form_state) {
  $form = array();
  $form['#attributes'] = array('enctype' => "multipart/form-data"); # Not sure if needed. Left over from my experiments with the file element.

  $form['banner'] = array(
    '#type' => 'image_upload_element',
    '#title' => 'Banner',
    '#default_value' => frontpage_load_file(variable_get('frontpage_banner_fid', 0)),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save')
  );
  
  return $form;
}
function frontpage_admin_settings_submit($form, &$form_state) {
  $banner_fid = 0;
  if (is_object($form_state['values']['banner'])) {
    $banner_fid = upload_element_save($form_state['values']['banner'], 'frontpage');
  }
  
  variable_set('frontpage_banner_fid', $banner_fid);
}

function frontpage_load_file($fid) {
  $result = db_query("SELECT *FROM {files} WHERE fid = %d LIMIT 1", $fid);
  
  if ($file = db_fetch_object($result)) {
    return $file;
  }
  else {
    return '';
  }
}
ericpai’s picture

Thanks for the code TwiiK , that's just what I needed.
Have you got the file size validator working? It's not checking for the size for me. I'm using the same functions frontpage_admin_settings_submit() and frontpage_load_file() as you have. Here is my code.

	$form['videoplayer_main_image'] = array(
		'#type' => 'image_upload_element',
		'#title' => t('Logo'),
		'#required' => TRUE,
		'#image_preview_size' => '100x100',
		'#image_preview_default_image' => drupal_get_path('module', 'cw') .'/no_image2.GIF',
		'#default_value' => videoplayer_main_load_file(variable_get('videoplayer_main_fid', 0)),
        '#file_validators' => array(
          'file_validate_size' => array(31384),
          'file_validate_extensions' => array('png'),
        ),
	); 

Also, is there a built in function to check file dimensions?

thanks