How to transfer the contents of a string, not a file, directly to a client via http download in Drupal?

I am stuck with form development in drupal; with a valid view, I want the form to initiate the transfer of the http file with the client, opening a prompt to download the file with the data received from the line in memory, and not with the file.

file_transfer ($ source, $ headers) looked pretty promising, but $ source is expected to be the URI for the file. Is there a similar function that accepts the contents of a string instead of a file URI? I have not found anything in my search through the Drupal API documentation yet.

I also tried the (hacker) more manual approach:

header("header statements");
header("more header statements");
echo $string_contents;
exit;

This method, of course, interrupts the processing of the Drupal form, which cannot lead to good things in the long run.

Any help would be greatly appreciated. Thank!

+3
source share
1 answer

Something like that:

<?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; 
?>

Drupal has a function that wraps header () and stats the headers in an array:

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_set_header/6

which can then be obtained with:

http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_get_header/6

+6
source

Source: https://habr.com/ru/post/1792859/


All Articles