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