4

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' => t('Managed file example'),
    '#type' => 'managed_file',
    '#upload_validators' => array(
        'file_validate_extensions' => array('jpeg jpg png gif'), 
        'MODULENAME_validate_not_filename' => array('test.')),
    '#upload_location' => 'public://imagebank',
    '#progress_indicator' => 'bar',
    '#progress_message' => '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' => 'plupload',
    '#title' => t('Upload files'),
    '#description' => t('This multi-upload widget uses Plupload library.'),
    '#upload_validators' => array(
    'file_validate_extensions' => array(
        'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'),
        'my_custom_file_validator' => array('some validation criteria'),
    ),
    '#plupload_settings' => array(
        'runtimes' => 'html5',
        'chunk_size' => '1mb',
    ),
);

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

'#upload_location' => '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

3

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, &$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, &$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");
    }
  }
1

so - upload works ?

and goes to public directory - or what ?

maybe check this thread here: http://drupal.org/node/1816438

4
  • Hi Rémy, upload works, but the file goes to the public directory indeed. I'll have a look at the link you send me.
    – jeroen
    Feb 14 '13 at 15:43
  • ou have to catch the submit and move that uploaded file yourself - as far as I see ..
    – rémy
    Feb 14 '13 at 15:46
  • Hi Rémy, found the solution. Your answer didn't quite cover the eventual solution so I can't award you with the bounty. I'm sorry for that.
    – jeroen
    Feb 15 '13 at 18:31
  • sad - should have taken more time ;)
    – rémy
    Feb 16 '13 at 9:43

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.