[development] How do I send a file to the user?

Ernst Plüss ernst.pluess at gmail.com
Thu Feb 1 06:41:23 UTC 2007


Thanks for your answers guis!

What I did so far:
1. generated the content of the excel file and store it in a variable
2. sent the appropriate http-header information
3. sent the excel file with print $exel_contet;

First this works fine for the user, he gets the download popup and
stores the file. But the next time the user clicks on a drupal link he
gets this:

warning: Cannot modify header information - headers already sent by
(output started at
C:\apachefriends\xampp\htdocs\istl\modules\simplenews\simplenews.module:1729)
in C:\apachefriends\xampp\htdocs\istl\includes\common.inc on line 269.

I'm sure to not have any spaces sent before my header information is
sent. But somehow drupal isn't happy that I'm sending header
informations myself.

This is my code:
    if ($where && $where_tid) {
      $query = 'SELECT DISTINCT s.mail, s.name FROM {sn_subscriptions}
s INNER JOIN {sn_snid_tid} t ON s.snid = t.snid WHERE ('.$where.') AND
('.$where_tid.')';
      $result = db_query($query);
      while ($mail = db_fetch_object($result)) {
        $mails[] = $mail;
      }
    }

    $data = "Name\tEmail\n";
    foreach($mails as $entry) {
        $line = simplenews_clean_value($entry->name);
        $line .= simplenews_clean_value($entry->mail);
        $data .= trim($line)."\n";
    }
    $data = str_replace("\r","",$data);
    header("Content-type: application/x-msdownload");
    header("Content-Disposition: attachment; filename=news_export.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$data";



Any ideas?
TIA Ernst


More information about the development mailing list