【问题标题】:download file, without saving in server下载文件,不保存在服务器中
【发布时间】:2011-02-12 09:57:00
【问题描述】:

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

我希望了解我需要什么 在此输入代码

【问题讨论】:

    标签: php header


    【解决方案1】:

    你只需要输出标题,然后是内容:

    header(...);
    
    echo $stringData;
    

    无需播放任何文件。

    【讨论】:

      【解决方案2】:
      header("Content-type: application/octet-stream");    
      header("Content-Disposition: attachment; 
      filename=\"$_fileName\"");
      ob_clean();
      readfile($_filePath);
      

      【讨论】:

        猜你喜欢
        • 2015-02-27
        • 2023-03-24
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 2020-09-20
        • 1970-01-01
        • 2016-10-01
        • 2011-09-14
        相关资源
        最近更新 更多