为Joomla写了一个自定义模块。用户在文本字段中输入文件名,并且应下载带有输入值作为名称的pdf文件。但是当点击该页面时,页面将被重定向到主页。这是代码
$ file = $ _POST ['posttext']。 '' 。 'PDF';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;
filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
尝试添加ob_flush()作为提问者建议但没有好处。该代码完美地作为一个单独的PHP文件。
答案 0 :(得分:0)
尝试以下
$app = JFactory::getApplication();
$file = $app->input->get('posttext') . '.pdf';
if (JFile::exists($file)) {
// File headers
header("Content-type: application/pdf");
header("Content-Disposition: attachment");
// File contents.
readfile($file);
}
如果您仍然遇到问题,可以检查Joomla中是否存在该文件,并使用window.open()