How do I set the custom upload directory for a plupload from field? My module uses a plupload form field. I'm trying to define a custom directory for my plupload uploads.

A normal upload form for a managed file looks something like this:

$form['managed_file'] = array(
    '#title' =&gt; t('Managed file example'),
    '#type' =&gt; 'managed_file',
    '#upload_validators' =&gt; array(
        'file_validate_extensions' =&gt; array('jpeg jpg png gif'), 
        'MODULENAME_validate_not_filename' =&gt; array('test.')),
    '#upload_location' =&gt; 'public://imagebank',
    '#progress_indicator' =&gt; 'bar',
    '#progress_message' =&gt; 'One moment while we save your file...',);


Plupload integrations module suggests using following code to integrate an Plupload form in a custom module:

$form['my_element'] = array( 
    '#type' =&gt; 'plupload',
    '#title' =&gt; t('Upload files'),
    '#description' =&gt; t('This multi-upload widget uses Plupload library.'),
    '#upload_validators' =&gt; array(
    'file_validate_extensions' =&gt; array(
        'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'),
        'my_custom_file_validator' =&gt; array('some validation criteria'),
    ),
    '#plupload_settings' =&gt; array(
        'runtimes' =&gt; 'html5',
        'chunk_size' =&gt; '1mb',
    ),
);


In a managed file form there is this parameter to set upload location uri:

'#upload_location' =&gt; 'public://imagebank',


In the plupload form I see no such parameter.

I'm still learning custom module development so the answer might be obvious, but it isn't for me atleast. Could someone please point me in the right direction? The files should be uploaded to public://imagebank
I eventualy found the solution in the plupload.module file itself. In there there is a page that can be found on folowing url: /plupload-test.

The form has a submit callback function:

/**
 * Submit callback for plupload_test form.
 */
function plupload_test_submit($form, &amp;$form_state) {
  $saved_files = array();
  $scheme = variable_get('file_default_scheme', 'public') . '://';
  // We can't use file_save_upload() because of
  // http://www.jacobsingh.name/content/tight-coupling-no-not
  // file_uri_to_object();
  foreach ($form_state['values']['pud'] as $uploaded_file) {
    if ($uploaded_file['status'] == 'done') {
      $source = $uploaded_file['tmppath'];
      $destination = file_stream_wrapper_uri_normalize($scheme . $uploaded_file['name']);
      // Rename it to its original name, and put it in its final home.
      // Note - not using file_move here because if we call file_get_mime
      // (in file_uri_to_object) while it has a .tmp extension, it horks.
      $destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
      $file = plupload_file_uri_to_object($destination);
      file_save($file);
      $saved_files[] = $file;
    }
    else {
      // @todo: move this to element validate or something and clean up t().
      form_set_error('pud', "Upload of {$uploaded_file['name']} failed");
    }
  }
}


I ended up using the same function in my custom module but adding my custom directory to the $destination the function now looks like this:

function plupload_files_submit($form, &amp;$form_state) {
  $saved_files = array(); //Array to capture file objects
  $scheme = variable_get('file_default_scheme', 'public') . '://';
  $directoryname = 'imagebank'; // Directory where images are collected
  $imagebankpath = $scheme . $directoryname;
  file_prepare_directory($imagebankpath, FILE_CREATE_DIRECTORY); // Create the folder if it doesn't exist
  // We can't use file_save_upload() because of
  // http://www.jacobsingh.name/content/tight-coupling-no-not
  // file_uri_to_object();
  foreach ($form_state['values']['uploadfiles'] as $uploaded_file) {
    if ($uploaded_file['status'] == 'done') {
      $source = $uploaded_file['tmppath'];
      $destination = file_stream_wrapper_uri_normalize($imagebankpath .'/'. $uploaded_file['name']);
      // Rename it to its original name, and put it in its final home.
      // Note - not using file_move here because if we call file_get_mime
      // (in file_uri_to_object) while it has a .tmp extension, it horks.
      $destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
      $file = plupload_file_uri_to_object($destination);
      file_save($file);
      $saved_files[] = $file;
    }
    else {
      form_set_error('uploadfiles', "Upload of {$uploaded_file['name']} failed");
    }
  }