下载文件,无需保存在服务器中

lol*_*ola 1 php header

或者用户可以下载文件而不将其保存在服务器上?我从数据库中获取数据,我想保存它们.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;
Run Code Online (Sandbox Code Playgroud)

}

如果没有这个代码应该怎么样:"$ fh = fopen($ file,'w');
fwrite($ fh,$ stringData); fclose($ fh);" 这个"unlink($ file);"

我希望在这里了解我需要的输入代码

Mic*_*hař 5

您只需输出标题然后输出内容:

header(...);

echo $stringData;
Run Code Online (Sandbox Code Playgroud)

无需使用任何文件.