I noticed the export-functionality doesn't work at all on some server setups. That is, clicking on "Export" just seems to do a page reload, but no file is downloaded (or shown).
This happens with all browsers (tested Chrome, FF and IE7). However, it works on my local development environment (which is a WIN/Xampp installation).

It was broken on our test-server and production server, which are both Unix/ZendServer. I did spend a few hours trying to debug and find the underlying reason for the download not working.
Tried all kind off different headers, cache settings etc. I think it might be caused by some outbut buffer option.

In the end I tested a simple die()-after the file output ... and that did the trick! Works fine in all environments :).

The fixed function in pubdlcnt.module, pubdlcnt_export_form_submit()-function looks like this now:

/**
 * Implementation of hook_submit().
 */
function pubdlcnt_export_form_submit($form, &$form_state) {

  $op = $form_state['values']['op'];
  if ($op == t('Export')) {
    $export_mode = $form_state['values']['pubdlcnt_export_mode'];
    // save the export mode
    variable_set('pubdlcnt_export_mode', $export_mode);

    $file_path = file_directory_temp() . '/public_download_count_' 
            . date('Y-m-d') . '.txt';
    export_counter_file($file_path, $export_mode);
    if (!file_exists($file_path)) {
      die("Error: File $file_path does not exists.");
    }
    if (!($fp = fopen($file_path, "r"))) {
      die("Error: Can not open file $file_path for reading.");
    }
    if (($file_size = filesize($file_path)) == 0) {
      die("Error: File $file_path is empty.");
    }

    // download (open dialog and let user save a file)
    header("Content-Disposition: attachment; filename=\"" 
      . basename($file_path) . "\"");
    header("Content-Length: $file_size");
    header("Content-Type: application/octet-stream"); readfile($file_path); 
    // delete the temporary file
    unlink($file_path);
	
    // Using die() to make sure there is no other output that can screw the download
    die();
    //return;
  }

Again, the only thing I changed was adding the die() (and a comment and //return).
No patch for now, I can create one if someone wants.

Comments

pixture’s picture

Thank you for your effort on this issue. It's very interesting that adding die() function fix the problem. I've researched the Internet and found some sample file download code which uses die() at the end (after readfile() function) just like you added.

Since die() and exit() are same, I guess that calling exit() function should also do the trick too. Since all my server works without die(), I can not confirm this. I will do a bit more research on this and add this code to the next release.

Again, thank you!!!

bibo’s picture

I'm glad this helped. Also, exit() is a much less grim command than the other angry sounding one ;)

cluke009’s picture

I was having the same problem as op and can confirm that this fix works.

In case it helps any I am running the latest Pressflow version on Redhat Enterprise with boost and memcache .

burt.lo’s picture

Just tried this on a Hot Drupal host, but it doesn't work. Weird. Any other ideas?