我必须使用PHP从MySQL数据库下载最新上传的PDF文件。该文件可以查看但保存到本地文件夹,而不是保存为.pdf,它会保存.php。并且.php文件包含编码数据。
有人可以建议我如何下载/保存.pdf文件吗?代码是:
<?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);
?>
答案 0 :(得分:0)
See this solution,转载于此:
添加ob_clean();和flush(); readfile()之前的函数;功能,可能是值得使用的东西,根据PHP手册中有关该主题的内容。
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
这些功能在您发布的代码中不存在
答案 1 :(得分:0)
我假设$ resu持有上传的文件名。那你为什么不链接到文件??
<?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'];
?>
<a href="http://YOUR UPLOAD FILE PATH/.<?php echo $resu?>.pdf">Download File </a>