dongou2019 2014-12-02 21:53
浏览 59
已采纳

使用PHP从数据库下载PDF文件[复制]

This question already has an answer here:

I have to download most recent uploaded PDF file from MySQL database using PHP. The file can view but while saving it to local folder, instead of saving it as .pdf , it saves .php. and that .php file contains encoded data.

Can anyone suggest how I download/save .pdf file? Code is:

<?php 
include 'connection.php';

  $sql=mysqli_query($connection,"Select name,content from ekalp where id = (select max(id) from ekalp)"); 
$result=mysqli_fetch_assoc($sql);
//$resu=$result['name']; 
$result=$result['content'];
echo $result."<br>";
$filename = $result.'pdf';
  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Accept-Ranges: bytes');
  ob_clean();
  ob_flush ();
  @readfile($filename);
 mysqli_close($connection);

?>
</div>
  • 写回答

2条回答 默认 最新

  • doushi1510 2014-12-02 22:00
    关注

    See this solution, reproduced here:

    Adding ob_clean(); and flush(); functions before the readfile(); function, could be something worth using, as per what the PHP manual states on the subject.

    readfile() http://php.net/manual/en/function.readfile.php

    ob_clean() http://php.net/manual/en/function.ob-clean.php

    flush() http://php.net/manual/en/function.ob-flush.php

    These functions are not present in your posted code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部