How can I save a file in specific folder? 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' =&gt; 'managed_file',
    '#upload_validators' =&gt; array('file_validate_extensions' =&gt; array('xml')),
    '#upload_location' =&gt; 'public://',
    '#submit' =&gt; array('custom_document_submit'),
    );
In form submit action you can give the destination path.

function formname_form_submit($form, &amp;$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