0

I've got a managed_file, with an #upload_location set to 'public://uploads'. When I upload files using the managed_file form element, uploaded files get copied to the 'public://' root, but not the subfolder 'uploads'.

I've got the below piece of code, which happily uploads files to 'public://', but not to 'public://uploads/'.

function mymodule_admin_form_submit($form, &$form_state) {

  $form['search_results']['file_upload'] = array(
    '#type' => 'managed_file',
    '#title' => t('Thumbnail'),
    '#default_value' => variable_get('file_upload'),
    '#upload_location' => 'public://uploads/',
  );  

  $form['#submit'][] = 'mymodyle_admin_form_submit';

  return system_settings_form($form);

}

/**
 * Form submit handler.
 */
function mymodyle_admin_form_submit($form, &$form_state) {

  if (!empty($form_state['values']['file_upload'])) {

    global $user;

    $file = file_load($form_state['values']['file_upload']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    file_usage_add($file, 'mymodule', 'user', $user->uid);    
    variable_set('file_upload', $file->fid);
    unset($form_state['values']['file_upload']);

  }
  else {
    $file = file_load(variable_get('file_upload', ''));
    if ($file->fid) {
      file_delete($file, TRUE);
    }
    variable_set('file_upload', '');
  }

}
  • 1
    does the uploads directory already exist? Are permissions properly set on that directory? – tenken Oct 13 '14 at 19:36
  • You have have two _form_submit() functions. The first function should be named _form() not _form_submit(). – Rich May 26 '15 at 4:25

Your Answer

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

Browse other questions tagged or ask your own question.