File uploads: upload location in submit function

3 replies [Last post]
Bruce Clothier
Offline
Last seen: 9 years 7 weeks ago
Joined: 2011-04-20

Hi.

Have been through all the "working with files" videos and now need something not covered (from what I can make out). Where your upload location is set in the managed files upload form, I need it to be set in the submit function as the directory is dependent on another field in the form. Do I have to change the $file->uri to what I want inside _submit()? How do I do this?

Thanks,
Bruce

Chris Shattuck
Offline
Last seen: 10 weeks 4 days ago
Joined: 2009-08-16
Hi Bruce, I haven't been

Hi Bruce,

I haven't been working in the file system for several months, so I'm a bit rusty there. But it does seem like changing the URI in the submit function would be a good thing to try. Just thinking off the top of my head, you should be able to test against the values in $form_state to decide where that file should go.

Keep us posted on if you get solution figured out for this.

Cheers!
Chris

Bruce Clothier
Offline
Last seen: 9 years 7 weeks ago
Joined: 2011-04-20
Solved

I used a bit of your code from unmanaged file uploads and combined with the managed.

First I commented out the #upload_location in the form and added the field on which the location would depend (for my purposes it was the client identifier).

$form['cid'] = array(
    '#type' => 'select',
    '#title' => t('Client'),
    '#options' => $clients,
    '#default_value' => $default_cid,
    '#required' => TRUE
  );

$form['managed_file'] = array(
    '#title' => t('File'),
    '#type' => 'managed_file',
    '#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
    // '#upload_location' => 'private://assets/managed',
    '#progress_indicator' => 'bar',
    '#progress_message' => 'One moment while we save your file...',
  );

Next, I changed the submit function, using some of the unmanaged file code to create the directory and move the file from TMP to its permanent position

if ($form_state['values']['managed_file'] != 0) {

    // Make the file permanent.
    $file = file_load($form_state['values']['managed_file']);
    $file->status = FILE_STATUS_PERMANENT;
    $directory = 'private://assets/managed/'. $form_state['values']['cid'];
    file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
    $file->uri = file_unmanaged_copy($file->uri, $directory, FILE_EXISTS_REPLACE);
    // reassign the $file->uri otherwise the managed file table entry shows the TMP location
    drupal_chmod($file->uri);
    file_save($file);

    // Need to add an entry in the file_usage table.
    file_usage_add($file, 'optitectPCA', 'image', 1);

    drupal_set_message(t("Your file has been uploaded!"));
  }

Chris Shattuck
Offline
Last seen: 10 weeks 4 days ago
Joined: 2009-08-16
Awesome, thank you so much

Awesome, thank you so much for sharing this!

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.