或者用户可以下载文件而不将其保存在服务器上吗? 我正在从数据库获取数据,我想将它们保存为 .doc (MS Word) 文件。
if(isset($_POST['save']))
{
$file = "new_file2.doc";
$stringData = "Text text text text...."; //This data put in new Word file
$fh = fopen($file, 'w');
fwrite($fh, $stringData);
fclose($fh);
header('Content-Description: File Transfer');
header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
unlink($file);
exit;
}
如果没有这个,代码应该是什么样子:
$fh = fopen($file, 'w');
fwrite($fh, $stringData);
fclose($fh);
还有这个
unlink($file);
您只需输出标题,然后输出内容即可:
header(...);
echo $stringData;
无需播放任何文件。
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment;
filename=\"$_fileName\"");
ob_clean();
readfile($_filePath);