douchuang4402 2019-02-26 22:45
浏览 140

php麻烦显示base64编码的pdf从数据库保存为blob

im storing pdf files as longblob in a db and want and want to open them as an attachment to a message in a blank.

heres the insert command:

$file = addslashes(file_get_contents($_FILES['file']['tmp_name']));
$filename = addslashes($_FILES['file']['name']);
$inexec = $pdo->prepare("INSERT into pm 
                (id, id2, title, user1, user2, message, file, 
                filename, timestamp, user1read, user2read, sender)
            values('$id', '1', '$title', '$userid', '$dn1recepid', '$message', '{$file}',
                    '{$filename}', '$time', 'yes', 'no', '$userid')")
        ->execute()

then in the message theres a button for attachments:

<a class="btn btn-light" href="downloadfile.php?filesrc=<?=$dn2['filename']; ?>" target="_blank">

which call this code:

$url=$_SERVER['REQUEST_URI'];
$parsed_url=parse_url($url);
parse_str($parsed_url['query'], $myvars);
$filename = $myvars['filesrc'];

$dlfile = $pdo->query("SELECT `file` FROM `pm` WHERE `filename`='$filename'");

$count = $dlfile->rowCount();


if($dlfile->rowCount() == 1)
{
  $result = $dlfile->fetch(PDO::FETCH_ASSOC);
  $file = base64_encode($result['file']);

  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Accept-Ranges: bytes');
  readfile($file);
}
else
{
die('<h1>Der Download ist gescheitert</h1>');
}

im grabing my filename from the url and search the db for the assosiated file. here is were it stop working. i find the file, but once i open the link a new tab opens with a pdf layout, but i get an error message: "error while loading pdf document".

i checked out the var_dump($file) and its still a string of random stuff, but im no pro, so i dont know how to proceed from here. i tried different command from other treads on this page, but nothign seemed to work. any help would be greatly appreciated!

best regards jchanni

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行