эй, я хочу создать кнопку, которая просматривает PDF-файлы, хранящиеся в базе данных MySQL, в браузере, поэтому мой код выглядит следующим образом:
. Это файл .php, который показывает список всех файлов pdf в базе данных с 2 кнопками загрузки / просмотра
<code>$sql = 'SELECT `id`, `name`, `type`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['type']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
<td><a href='get_file.php?id={$row['id']}'>Download</a></td>
<td><a href='view.php?id={$row['id']}'>View</a></td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}
";
}
// Закрыть соединение mysql
$ DbLink-> близко ();
?>
и это view.php, который запускается кнопкой View:
<?php
$id = intval($_GET['id']);
$file = "
SELECT `type`, `name`, `size`, `data`
FROM `file`
WHERE `id` = {$id}";
header('Content-type: application/pdf');
header('Content-Disposition: inline');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
header("Location: $file");
@readfile($file);
?>
проблема в том, что всякий раз, когда я нажимаю кнопку просмотра, я получаю сообщение об ошибке "file does not start with %PDF-" although i am sure that the files in the database are of type pdf.
so anyone can solve this problem?