I'm trying to save a file that the user has uploaded using my form.

My form:

$form['fifth']['field_cv'] = array(
        '#title' => t('CV'),
        '#type' => 'managed_file',
        '#size' => 19,
        '#upload_location' => 'public://',

My validator using hook_validate:

$file = file_load($form_state['values']['fifth']['field_cv']);
// Change status to permanent.
if($file) {
    $file->status = FILE_STATUS_PERMANENT;
    file_usage_add($file, 'popover', 'document', $file->fid);
else {
    form_set_error('CV', t('Er is geen cv geüpload'));

So now in hook_submit how do I add this to the user? Do I need to create an extra database field for this? And should I save only the $fid ( file id ) in this field?


Is there a better way so drupal takes care of this and also revisions of this field are saved?

building this in D7


I'm not sure how your collecting this CV (apart of registration process, on your own custom page, apart of the user profile page... whatever).

I would add a field to User Accounts at /admin/config/people/accounts/fields, or maybe install the Profile2 module for D7 if you have alot of types of Users.

After you call file_save($file) if you use the Devel module you'll see that $file should have a new value fid so you can then load a user account (or Profile2 profile) and give the CV $file->fid as the File field value.

| improve this answer | |
  • yes I'm using devel and I see a new fid. so the best way is to create a new field /admin/config/people/accounts/fields using and store the fid in it? There is no special file handling field? – FLY Feb 6 '13 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.