You are here

function media_browser_plus_download_multiple_files in Media Browser Plus 7.3

Download multiple files.

Creates n archive for multiple files - directly sends a single file.

Parameters

array $files: A list if file id's or file objects.

1 call to media_browser_plus_download_multiple_files()
media_browser_plus_download_action_form in ./media_browser_plus.module
Configuration form shown to the user before the action gets executed.
1 string reference to 'media_browser_plus_download_multiple_files'
media_browser_plus_menu in ./media_browser_plus.module
Implements hook_menu().

File

./media_browser_plus.module, line 231
Media Browser Plus - enhanced file management functions.

Code

function media_browser_plus_download_multiple_files($files) {
  $fids = array();

  // Check if the list consists of /contains file ids.
  foreach ($files as $key => $file) {
    if (!is_object($file)) {
      $fids[] = $file;
      unset($files[$key]);
    }
  }

  // If file ids were found populate list of file objects.
  if (!empty($fids)) {
    $files = array_merge($files, file_load_multiple($fids));
  }
  if (count($files) > 1) {
    $file_name = 'file_download_' . time() . '.zip';
    $archive = drupal_tempnam('temporary://', 'mbp');

    // Abuse the existing archiver action. Do like we run an action ;)
    module_load_include('inc', 'views_bulk_operations', 'actions/archive.action');
    $archiver_context['destination'] = $archive;
    $archiver_context['progress']['current'] = 1;
    $archiver_context['progress']['total'] = count($files);
    $archiver_context['settings']['temporary'] = TRUE;
    foreach ($files as $file) {
      views_bulk_operations_archive_action($file, $archiver_context);
    }

    // Register cleanup function. The created archive has to be removed again.
    drupal_register_shutdown_function('media_browser_plus_download_action_cleanup');
  }
  elseif (count($files) == 1) {
    $file = reset($files);
    $archive = $file->uri;
    $file_name = drupal_basename($file->uri);
  }
  else {
    drupal_not_found();
    drupal_exit();
  }

  // Ensure we've the latest file information.
  clearstatcache();

  // Prepare headers.
  $headers['Pragma'] = 'public';
  $headers['Expires'] = '0';
  $headers['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0';
  $headers['Content-type'] = 'application/zip';
  $headers['Content-Disposition'] = 'attachment; filename=' . $file_name;
  $headers['Content-length'] = filesize($archive);
  file_transfer($archive, $headers);
}