当我将pdf文件上传到数据库然后我将其下载时,它无法打开,因为它已损坏。我的脚本中有什么问题吗? 在数据库中,我有两行,“field_name”是BLOB,id是主键。
<?php
function pripoj()
{
$connect=mysqli_connect ('---','----','******','testovaciukol');
if (mysqli_connect_errno())
{
die ("Failed to connect to MySQL: " . mysqli_connect_error());
}
return $connect;
}
function sql($pripojeni,$prikaz)
{
$vysledek=mysqli_query($pripojeni,$prikaz);
echo mysqli_error();
return $vysledek;
}
function zavri($pripojeni)
{
mysqli_close($con);
}
?>
<form method="post" enctype="multipart/form-data">
File: <input type="file" name="pdf" id="pdf" accept="application/pdf"
title="Choose File" ><br>
<input type="submit" name="upload" id="upload" value="Upload" ><br>
<input type="submit" name="read" id="read" value="Read" >
</form>
<?php
$link=pripoj();
if(isset($_POST['upload'])) {
$file_path=$_FILES['pdf']['tmp_name'];
$file_type=$_FILES['pdf']['type'];
$file_size=$_FILES['pdf']['size'];
$file_name=$_FILES['pdf']['name'];
if ($file_name != "" && $file_type == 'application/pdf') {
$data=mysqli_real_escape_string($link, file_get_contents($file_path));
$query="INSERT INTO `testovaciukol`.`pdf` SET field_name = '".$data."'";
$result = sql($link, $query); //query execution
if($result)
echo 'Success! Your file was successfully added!';
else
echo 'Error!';
} else {
echo 'Not a pdf file';
}
}
if(isset($_POST['read'])) {
$sql = "SELECT field_name FROM pdf WHERE id = '1'";
$result2 = sql($link, $sql);
$row = mysqli_fetch_object($result2); resultset
$pdf_content = $row->field_name;
$fileName = time().".pdf";
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=".$fileName);
print $pdf_content;
}
?>