I recently worked with this problem, and it is important to give an answer after the test solutions provided by us. Then I solved this problem as follows. To generate fpdf, I used this code. :
$content = $pdf->Output("", "S");
// addslashes is the key to store blob file
$data = addslashes($content);
$name = "testname.pdf";
$mime = "application/pdf";
$extficheiro = "pdf";
$nomeficheiro = "miarchivo";
$size = strlen($content);
$query = "INSERT INTO filetable(name, mime, size, data, created)
VALUES ('$name','$mime', '$size', '$data', NOW())";
$conn->query($query);
To read (retrieve from the database) from my database, it was preferable to use the pdo code:
if(filter_has_var(INPUT_GET, "var") !== false && filter_input(INPUT_GET, 'var', FILTER_VALIDATE_INT) !== false)
{
$var = filter_input(INPUT_GET, "var", FILTER_SANITIZE_NUMBER_INT);
try {
$dbh = new PDO("mysql:host=our_host;dbname=database", 'username', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM filetable WHERE id= ".$var;
$stmt = $dbh->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$array = $stmt->fetch();
header("Content-Type: ". $array['mime']);
header("Content-Length: ". $array['size']);
header("Content-Disposition: inline; filename='". ($array['name'])."'");
echo $array['data'];
}
catch(PDOException $e)
{
echo $e->getMessage();
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
else
{
echo '0';
}
These codes worked great. I hope that this contribution will help others to give up the headache with this problem. Regards.
source
share