How to display blob with PHP? - php

Why can't my php display the image?
<?php
include("sql.php");
//$sql = "SELECT * FROM filesdb WHERE fileid = 5";
$id = $_GET['id'];
// do some validation here to ensure id is safe
$sql = "SELECT * FROM filesdb WHERE fileid =$id";
echo $sql."<br>";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//header('content-type: image/jpeg');
echo "<br>Fileid:".$row['fileid'];
echo "<br>FileName:".$row['filename'];
header("Content-Type: image/jpeg");
echo "<br>".$row['dbforfile'];
}
it can display fileid and filename, but not dbfofile, which is a BLOB.

If you're downloading the file contents, you must not put HTML elements around it.
You can also only send one image at a time, so there's no point in looping.
<?php
include("sql.php");
//$sql = "SELECT * FROM filesdb WHERE fileid = 5";
$id = $_GET['id'];
// do some validation here to ensure id is safe
$sql = "SELECT dbforfile FROM filesdb WHERE fileid =$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
header('content-type: image/jpeg');
$row = $result->fetch_assoc();
echo $row['dbforfile'];
}

You are only doing one image, so you don't need a loop. But if you want to do multiples; you need to use an img tag and call another PHP script as if it were an image in the src and have that script output the image (with headers). Or you can use base64 encoded data as the image source in the tag:
while($row = $result->fetch_assoc()) {
echo "<br>Fileid:".$row['fileid'];
echo "<br>FileName:".$row['filename'];
echo "<br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['dbforfile']).'"/>';
}

Related

Rebuilding a jpg from blob

Edited and started over to better describe my issue.
I have this code that displays an image from a database using blob data.
function getContent($db) {
$query = "SELECT name, animalid, image, thumb FROM images ";
$sql = $db->prepare($query);
$sql->execute();
return $sql->fetchAll();
}
$data = getContent($db);
foreach($data as $row) {
$id = $row['id'];
$image = $row['image'];
$thumb = $row['thumb'];
echo '<img src="data:thumb/jpg;charset=utf8;base64,'.$image.'">';
}
This code displays a image on the page, when viewing the source code i see the blob data that the image is made from, what i need is to actually make an image that i can name properly.
How do i make a ******.jpg from the blob data?
Use base64_encode. You should use image/jpeg MIME type without charset.
function getContent($db) {
$query = "SELECT name, animalid, image, thumb FROM images ";
$sql = $db->prepare($query);
$sql->execute();
return $sql->fetchAll();
}
$data = getContent($db);
foreach($data as $row) {
$id = $row['id'];
$image = $row['image'];
$thumb = $row['thumb'];
// $image is the BLOB
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'">';
}

Download doc from MySQL using PHP

for some reason after I click the url to download, it happen to view the file on my browser which results it massive symbols and random letters. Is there any way to get it to download rather than viewing it on browser? I'm really new to programming, you guidance would really be helpful. Thank you.
<?php
mysql_connect("localhost","root","");
mysql_select_db("tuitioncentre");
if(isset($_GET['id'])) { // if id is set then get the file with the id from database
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM files WHERE id = $id";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =
mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content; exit;
}
?>
<?php
$query = "SELECT id, name FROM files";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<?php echo $name; ?>
<?php
}
}
?>
This is example of the output.
https://i.stack.imgur.com/Cx6XR.png

Having trouble opening blob image from mysql with php

I'm kind of a noob so I don't know if I'm missing something small. The images are uploaded to the database just fine, but using this just gets me a blank square. I don't know if I'm missing something obvious?
$name = $_SESSION['username'];
$sql="SELECT pic FROM userinfo WHERE username = '$name'";
$result = mysqli_query($link,$sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$pic = base64_encode($row['pic']);
echo "<img height='300' width='300' style='padding-left:10px;' src='data:image/jpeg;base64,".$pic."'>";
}
}
The upload code, I'm 95% sure that the issue is somewhere in the upload process. I commented out what I was previously using to send the query.
$name = $_SESSION['username'];
$bio = $_POST['bio'];
$sql="UPDATE userinfo SET pic='$pic' WHERE username='$name'";
$stmt = $link->prepare($sql);
$stmt->bind_param("b", $pic);
$pic = base64_encode($_FILES['fileToUpload']['tmp_name']);
//$query = mysqli_query($link,$sql);
$stmt->execute();

Displaying a date specific image from a databse

The aim is for a customer to upload their advert to the database, that triggers and email for approval. Once approved ad will display in location between the set dates.
I have written the code correctly to display by date and approval from an admin area. I just cannot get the image to display.
The code below details the process so far.
<?php
include('mysql_connect.php');{
$location='1';
}
$resultSet = $mysqli->query("SELECT * FROM adverts WHERE adloc = '$location' AND approval ='Y' ");
if($resultSet->num_rows > 0){
while($rows = $resultSet->fetch_assoc())
{
$id = $rows ['id'];
$start = $rows ['start'];
$end = $rows ['enddate'];
$business = $rows ['business'];
$email = $rows ['email'];
$tel = $rows ['tel'];
$web = $rows ['web'];
$advert = $rows ['image'];
$Date = date('Y-m-d');
$Date=date('Y-m-d', strtotime($Date));;
$DateBegin = date('Y-m-d', strtotime("$start"));
$DateEnd = date('Y-m-d', strtotime("$end"));
}
if (($Date > $DateBegin) && ($Date < $DateEnd))
{
echo "ADVERT";
echo '<img src="getad.php?id=$id">';
}
else
{
echo "FILLER IMAGE";
}
}
?>
The code for the getad.php file is as follows
if(isset($_GET['id']))
{
$id = mysqli_real_escape_string($_GET['id']);
$query = mysqli_query("SELECT * FROM adverts WHERE id= '$id'");
while ($row = mysqli_fetch_assoc($query))
{
$image = $row['image'];
}
header("content-type: image/jpeg");
echo $image;
}
else
{
echo "Error!";
}
?>
I am sure I can't be far off. The image is a JPEG LONGBLOB in the database.
Thank you.
Firstly, this is failing you:
$id = mysqli_real_escape_string($_GET['id']);
$query = mysqli_query("SELECT * FROM adverts WHERE id= '$id'");
Both of those functions require a database connection be passed and as the first parameter:
$id = mysqli_real_escape_string($mysqli, $_GET['id']);
$query = mysqli_query($mysqli, "SELECT * FROM adverts WHERE id= '$id'");
References:
http://php.net/manual/en/mysqli.real-escape-string.php
http://php.net/manual/en/mysqli.query.php
However, may need to use base64_decode:
header("content-type: image/jpeg");
echo base64_decode($image);
or
echo '<img src="data:image/gif;base64,' . $image . '" />';
Here is an example borrowed from https://stackoverflow.com/a/20564797/
$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
For debugging purposes:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
As well as or die(mysqli_error($mysqli)) to mysqli_query().

How to load image from the database in html?

This is my code.
<img src="getimage.php?id=1" alt="Delicious World" />
This is getimage.php
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("db_cupcake");
$sql = "SELECT image FROM item WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['0'];
?>
I have directly stored the image in the database. I want to load those images in the webpage. Help me out.
First things first . If you are passing variables in the mysql query , then use quotations .Only then mysql will execute the query .
your query :- $sql = "SELECT image FROM item WHERE id=$id";
Instead use this query :- $sql = "SELECT image FROM item WHERE id='$id'";
And to display the image you can use
echo $row['image'];
You need to echo $row[image]; not echo $row[0];
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "", "db_cupcake");
$result = mysql_query("SELECT image FROM item WHERE id=$id");
$row = mysql_fetch_assoc($result);
header("Content-type: image/jpeg");
echo $row['image'];
mysql_close($link);
?>

Categories