You are here

function media_browser_plus_download_images_submit in Media Browser Plus 7

Same name and namespace in other branches
  1. 7.2 media_browser_plus.module \media_browser_plus_download_images_submit()

Puts all selected media items into a zip archive and sends it as download.

@TODO: check for internet sources etc. Only local files should be parsed.

_state

Parameters

$form:

1 string reference to 'media_browser_plus_download_images_submit'
media_browser_plus_media_basket_form in ./media_browser_plus.module
Appends the media basket.

File

./media_browser_plus.module, line 115
Adds fields to the media browser forms for better UX

Code

function media_browser_plus_download_images_submit($form, &$form_state) {
  if (isset($form_state['input']['selected_media']) && media_access('download')) {
    $ids = array_keys($form_state['input']['selected_media']);

    // only load those
    $conditions[] = array(
      'property' => array(
        'fid',
        array(
          $ids,
        ),
        'IN',
      ),
    );
    $media_entities = media_browser_plus_load_multiple(array(
      'conditions' => $conditions,
      'apply_filter' => FALSE,
      'paging' => FALSE,
    ));

    // Create archive.
    apache_setenv('no-gzip', '1');
    $name = 'media-download-' . md5(microtime() . uniqid());
    $zip_file = '/tmp/' . $name . '.zip';
    $zip = new ZipArchive();
    $res = $zip
      ->open($zip_file, ZipArchive::CREATE);
    if ($res === TRUE && count($media_entities->results)) {
      foreach ($media_entities->results as $media) {
        $zip
          ->addFile(drupal_realpath($media->uri), $media->filename);
      }
      $zip
        ->close();

      /** OLD
          header('Content-type: application/zip');
          header('Content-Disposition: attachment; filename=' . $zip_file);
          header('Content-Length: ' . filesize($zip_file));
          header('Pragma: no-cache');
          header('Expires: 0');
          */
      header('Cache-Control: public');
      header('Pragma: public');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Cache-Control: public');

      // header('Content-Description: File Transfer');
      // header('Content-type: application/zip');
      header('Content-Disposition: attachment; filename=' . $zip_file);

      // header('Content-Transfer-Encoding: binary');
      header('Content-length: ' . filesize($zip_file));
      readfile($zip_file);
      unlink($zip_file);
      die;
    }
    else {
      drupal_set_message(t('Failed to create download archive'), 'error');
    }
  }
}