I want to get a PDF File from a BLOB in my MySQL Database. The PHP for showing the PDF works fine:
<?php
$mysqli = new mysqli("192.168.1.11", "root", "password", "DB");
if ($mysqli->connect_errno) {
die("Connection refused: " . $mysqli->connect_error);
}
try{
$sql = "SELECT file FROM table WHERE id = 1";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_object($result);
header('Content-type: application/pdf');
echo $row->file;
}catch(Exception $e){
echo "caught exception: ", $e->getMessage(), "\n";
}
?>
Now i want to embed this PDF File into a HTML. I tried something like this:
<?php
$pdf = "MY_PDF_FILE.PDF"; ◄
echo <<<BLOCK
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Embeded PDF</title>
</head>
<body>
<embed src="$pdf" type="application/pdf" height="25%" width="25%">
</body> ▲
</html>
BLOCK;
?>
I tried to save the $row->file; in the $pdf with $pdf = $row->file;. Always when i try to call the index.php of my website, it shows up, that the PDF File cannot be displayed and i only see the browsers PDF Viewer.
Can someone help me with this? :/
you can use an iframe element to show PDF.
You can load your base 64 encoded blob to data attribute of iframe.
Inside your data attribute you need to write: 'application/pdf;base64,' before base64 string.
<iframe data="data:application/pdf;base64,B64STRINGHERE" type="application/pdf" style="height:200px;width:60%"></iframe>
So you have to do something like:
<?php
// Connection to DB + Retrive blob
// We assume that your '$row->file' contains Blob data.
// So encode '$row->file' to base 64
$pdf = base64_encode($row->file); // BLOB TO BASE64
echo <<<BLOCK
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Embeded PDF</title>
</head>
<body>
<iframe data="data:application/pdf;base64,$pdf" type="application/pdf" style="height:200px;width:60%"></iframe>
</body>
</html>
BLOCK;
?>
Related
<?php
$cn= pg_connect('host=localhost port=5432 dbname=sampledb user=postgres
password=xxxx');
$id = $_GET['id'];
$query=pg_query($cn,"select * from books where id=$id");
while($row=pg_fetch_array($query))
{
$id = $row['id'];
$book_pdf=$row['book_pdf'];
$pdf=pg_unescape_bytea($row['book_pdf']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<object data="data:application/pdf;base64,<?php echo $pdf; ?>"
type="application/pdf" style="height:1000px;width:100%">
</object>
</body>
</html>
I encode my PDFs in base64 format and then save it in PosgreSql bytea column and then I give pg_unescape_bytea() to extract base64 data. Can someone tell how to handle large amount of base64 data.
I'm trying to create an image in PHP, while also giving the image a title and meta tags. I'm using the code below but the HTML does nothing. I do see the image though.
<?php
$IMGID = $_GET['id'];
$im = imagecreatefrompng($IMGID . ".png");
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
<html>
<head>
<title>test</title>
</head>
<body>
</body>
</html>
Any ideas?
My video won't play in my browser I've tried different file types and it didn't change anything I can enter where the file is uploaded on my computer like uploaded/video name and it will show but when I try to get it off my db with the tags it says No video with supported format or MIME type found and when I try to get the video with the embed tag it says a plugin is needed to display this content
I am connected to my db at the top of this page too I just didn't include that here
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Watch</title>
</head>
<body>
<video controls="controls" width="550" height="315">
<source src="<php $url ?>">
</video>
<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM `videos` WHERE id='$id'");
while($row = mysql_fetch_assoc($query))
{
$name = $row['name'];
$url = $row['url'];
}
echo "Your are watching ".$name."<br />";
//echo "<embed src= '$url' width='560' height='315'></embed>";
}
else
{
echo "Error!";
}
?>
</body>
</html>
I replaced my commented out embed tags with <video> tags and it worked. I fixed this not long after I posted, but I'm leaving an answer for people who have this problem.
i add this define() function in my config.php file.
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
Now, i required config.php file into my index.php page like this :
require $abspath .'/config.php';
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
now in output i have this result:
?????????????????????
worked result :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
<?PHP echo _SIGN_; ?>
</body>
</html>
output result :
تواصل معنا
how do fix this for show unicode UTF-8 when i required config.php ?!
Fist where did you set the variable $abspath ?
Then you have to put <?php and ?> tags around your require statemet and in your config file around your php!
Now the example below works fine for me and is pretty much the same as yours!
config.php:
<?php
$options['0']['sign'] = "تواصل معنا"; //$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_', $options['0']['sign']);
?>
index.php:
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
And the Output is:
تواصل معنا
Did you try after establishing connection to the database execute:
set_charset ( "utf8")
I use MySQLi and make it this way:
//-> conncecting to db
$mysqli = new mysqli ( $hostname, $username, $password, $db );
if ( $mysqli -> connect_errno ) {
echo 'Failed to connect to MySQL: ' . $mysqli -> connect_error;
}
// change character set to utf8
if ( ! $mysqli -> set_charset ( "utf8") ) {
printf ( "Error loading character set utf8: %s\n", $mysqli->error );
}
I have a Products_Images table which stores the images in BLOB format. I want to show all the images and product details in 'View All Products' page. To accomplish this task I have created the following program, but its not giving me the desired output. Kindly check it.
Thanks.
<?php
include 'connect.php';
$query= "select distinct product_id, image from product_images";
$query_run= mysql_query($query);
while ($query_fetch= mysql_fetch_assoc($query_run))
{
echo '</br>';
echo $query_fetch['image'];
}
header("Content-type: image/jpeg");
?>
Displaying The Image
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<br />
<img src="all_images.php" alt="Image" width="50" height="50" />
<body>
</body>
</html>
When you are using header() function you cannot print anything prior to a header call as this directs the web server to prepare a content header
<?php
include 'connect.php';
$query= "select distinct product_id, image from product_images";
$query_run= mysql_query($query);
header("Content-type: image/jpeg");
while ($query_fetch= mysql_fetch_assoc($query_run))
{
// echo '</br>';
echo $query_fetch['image'];
}
?>
Note: Please avoid using mysql* they are depreciated in php newer
version keep your hands on using PDO or at least mysqli
why you store it in database, store your image in a folder and enter the name in the database, while calling the image in the src just write the path of the folder and php code where you mention the name.
<img src="yourfolder/<?php echo your database table field name where images names are stored?>" />