
Interesting, I didn’t know one could do that, i’ll investigate, guess I need ’application/octet-stream’...
... you can add custom types in the Manager - Administration - System Configuration, at the bottom of the Site settings tab.
You could also make a snippet to get the data, generate and write a file, then "return" a link to the file for downloading.
// do database stuff, process it, write file myfile.csv to the assets/files folder // (that folder will probably need to be world-writable, 777 unless you're running PHP as fastCGI and suexec). $output = '<a href="assets/files/myfile.csv">myfile.csv</a>'; return $output;
In your snippet, you can do the database query, process the result, write it to a file, then generate a link to the file and return the link:
...
now wherever you put the snippet that link will show on the page.
<body onLoad="javascript:window.location.href='data.csv'">
- final step: push the flat file to the visitor, problem: how do you trigger this without intervention??
return $modx->sendRedirect(path/to/your/download.php?fileName=file.txt);
$filename = $_GET['fileName'];
$path = 'path/where/the/file/was/created/';
header('Pragma: private');
header('Cache-control: private, must-revalidate');
if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT')) or
preg_match('#MSIE ([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT'))) {
header("Content-type: application/octetstream");
} else {
header("Content-type: application/octet-stream");
}
$fp = fopen($path.$filename,"r");
$filedata=fread($fp,filesize($path.$filename));
fclose($fp);
if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT')) or
preg_match('#MSIE ([0-9].[0-9]{1,2})#', getenv('HTTP_USER_AGENT'))) {
header("Content-type: application/octetstream");
} else {
header("Content-type: application/octet-stream");
}
header("Content-Disposition: attachment; filename=".$filename);
print $filedata;
Simply changing the content disposition from inline to attachment, will send the proper headers so the browser will download the file instead of trying to render it. Is this what you are looking for?
I use this all the time to present links to downloadable CSV files, or any other file type I can generate from a MODx page.