0

I want to upload files to specific folders. How can I achieve this programatically.
How can I modify below code such that upload_location should be dynamic.

    $form['document'] = array(
    '#type' => 'managed_file',
    '#upload_validators' => array('file_validate_extensions' => array('xml')),
    '#upload_location' => 'public://',
    '#submit' => array('custom_document_submit'),
    );
  • How you wants to pass folder name? do you want to pass the folder name as a form element. – DRUPWAY Jul 27 '16 at 10:58
  • I have some folders in public://, so I am thinking to display all folder names in dropdown, then user can select the folder name and next upload option is there. When user selects folder and uploads files, it should go to that perticular folder. So how you will suggest me to do? – Suraj Jul 27 '16 at 11:03
1

In form submit action you can give the destination path.

function formname_form_submit($form, &$form_state) {
    $destination = $form_state['values']['foldername_name'];
    $validators = array();
    $file = file_save_upload('file', $validators, 'public://'.$destination);
}

More about file_save_upload get https://api.drupal.org/api/drupal/includes!file.inc/function/file_save_upload/7.x.

more related links https://stackoverflow.com/questions/1253094/howto-drupal-file-upload-form

  • Thank you for this. Is there any way to pass dynamic folder name in the form element only.? – Suraj Jul 27 '16 at 12:01
  • i think you want a input box for folder name right? – DRUPWAY Jul 27 '16 at 12:15
  • I have input box and i have one button, when I enter name and click a button, it will create a folder in public//:. But now i want to upload the files to the newly created folders. – Suraj Jul 27 '16 at 13:29
  • are you creating folder on form submit button or another button present folder create. – DRUPWAY Jul 27 '16 at 13:36
  • for that form submit I am calling seperate submit function, not default form submit. – Suraj Jul 27 '16 at 13:48

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.