How to transfer contents of a string, not a file, directly to client via http download in Drupal?
I'm getting stuck while developing a form in drupal; upon valid submission, I want the form to initiate an http file transfer with 开发者_运维知识库the client, opening a file download prompt with data generated from an in-memory string, not a file.
file_transfer($source, $headers) looked quite promising, but $source is expected to be a URI to a file. Is there a similar function that accepts the contents of string instead of a file URI? In my search through the Drupal API documentation, I have yet to find anything.
I have also tried a (hackish) more manual approach:
header("header statements");
header("more header statements");
echo $string_contents;
exit;
This method of course, interrupts Drupal's form processing, which can't lead to good things in the long term.
Any help would be much appreciated. Thanks!
Something along these lines:
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"my-file.txt\"");
$data="Hey this is a text file, yay \n";
$data .= "it has stuff in it";
echo $data;
?>
in Drupal there is a function that wraps header() and stores headers statically in an array:
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_set_header/6
which can then be retrieved with:
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_get_header/6
精彩评论