Hi, hope someone has tried this before. I’m trying to get a doument to represent a pdf file.
I’ve created a TV called ’File’ that allows a filepath to be associated with a document. I have set the document content-type to ’application/pdf’ and the content-disposition to ’attachment’ in the page settings tab. What I’m not clear about is how to output the file to the page, given that I can’t just output the headers directly as the page is parsed by modX. So I wrote this snippet to open the file and just echo the file data:
global $base_path;
/* Parameters */
$file = isset($file)? "$file" : '';
if ($file != "") {
$filename = $base_path.$file;
}
$dataFile = fopen( $filename, "r" ) ;
if ( $dataFile ) {
while (!feof($dataFile)) {
$buffer = fgets($dataFile, 4096);
echo $buffer;
}
fclose($dataFile);
} else {
return("fopen failed for $filename");
}
return;
EDIT: Added code tags
But unsurprisingly, it mangles the modX document parser.
One solution would be to just use modX->SendRedirect(’$filePath’) so that the page just redirects to the file, but that seems somehow clumsy to me. It makes me think I might be missing something obvious here? Is there a simple way of representing files in the document tree?
Can anyone enlighten me?