Downloading image stored as a BLOB does not work - php

My script returns a file which has a proper name but it is without any filetype or extension. When I open this file it shows just very long almost infinite string of characters. I want to return an image (.png file) and I specified MIME type for it in
header("Content-type: image/png");
My script is:
$query = "SELECT * " ."FROM images WHERE filename = '$name2'";
$result = mysqli_query($connect,$query)
or die('Error, query failed');
list($file, $content) = mysqli_fetch_array($result);
header("Content-type: image/png");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
mysqli_close($connect);
exit;
And the echoed file is just infinite string of characters like that:
¸A<'^ą~`U–°ŹĘŘC.
It doesn't convert the BLOB into proper filetype (.png)

<?php
$connection = mysqli_connect("localhost","root"," ",your_database)
or die('Database Connection Failed');
mysqli_set_charset($connection,'utf-8');
$id = 1;
// Use a prepared statement in production to avoid SQL injection;
// we can get away with this here because we're the only ones who
// are going to use this script.
$query = "SELECT * " ."FROM images WHERE filename = '$name2'";
$result = mysqli_query($connect,$query)
or die('Error, query failed');
list($id, $file, $type, $size,$content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
mysqli_close($connection);
exit;
?>
It is worked for me!!

Related

downloading Blob files with a link or a button

I found this piece of PHP code to download Blob files, but it starts downloading automatically after I load the page. Can someone show me how to make that happen with a button click?
$query = "SELECT * " .
"FROM tbl_uploads WHERE id = '$id'";
$result = mysqli_query($connection,$query) or die('Error, query failed');
list($id, $file, $type, $size,$content) = mysqli_fetch_array($result);
//echo $id . $file . $type . $size;
//echo 'sampath';
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
You can use the following HTML code to download Blob files with a link:
Download
Put the code that you've posted above into download.php and this should work.

unable to download file from my mysql database

I'm trying to download file from MySQL server, but am not able to do so.
The script executes but it can download only the first 65 KB of the file.
<?php
include('connect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$query = "SELECT * FROM upload WHERE id = '$id'";
$result = mysqli_query($connection,$query) or die(mysql_error());
while($row = mysqli_fetch_assoc($result))
{
$name=$row['name'];
$size=$row['size'];
$type=$row['type'];
$content=$row['content'];
}
header("Content-disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;
} else {
die("No file id given...");
}
unset($_GET['id']);
?>
You're misusing content-length. It needs to be the size of what you transmit.
You want something like this:
header( "Content-length: " . count( content) );

Download empty BLOB file from MySQL DB PHP

I'm trying to download a XML file from my DB. The file is stores in a BLOB field, but when I open the page it dowloads a blank xml file.This is my function:
<?php
$conexao = mysql_connect("db","user","pss") or die('Not connected : ' . mysql_error());
mysql_select_db("db", $conexao) or die (mysql_error());
$result = mysql_query("select * from xmlNFe where xml_id = 1", $conexao);
$row = mysql_fetch_array($result);
$xml = $row['xml_xml'];
$nome = mysql_result($result, 0, 'xml_chNFe');
header('Content-type: application/octet-stream');
header('Content-Description: File Transfer');
header("Pragma: public");
header("Content-Disposition: attachment;filename=".$nome.".xml");
header('Content-Transfer-Encoding: binary');
header("Content-length: ".filesize($xml));
header('Accept-Ranges: bytes');
ob_clean();
flush();
echo $xml;
mysql_close($conexao);
exit;
?>
Does anyone have any idea?
header("Content-length: ".filesize($xml));
^^^^
$xml is your actual xml data, it's NOT a filename, so filesize() will FAIL and return boolean FALSE. You cannot get the size of a file which does not exist.
Try
header("Content-length: ".strlen($xml));
^^^^^^
instead.

unknown file name change when file downloads

am using a script to download files from my site.
if (isset($_GET['file'])){
$file = $_GET['file'];
$query = "SELECT fileurl FROM dbname WHERE id='$file'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$filename = $row[fileurl];
send_download($filename);
}
function send_download($filename){
$file_path = $filename;
$file_size=#filesize($file_path);
header("Content-Type: application/x-zip-compressed");
header("Content-disposition: attachment; filename=$filename");
header("Content-Length: $file_size");
readfile($file_path);
exit;
}
It works great but the only caveat is that the file name is changed to some encryted string ie asdajkhasdkahdkasdakfsdhf.pdf how do I change this so that it downloads with the original name??? And if possible, append + to the spaces inbetween the file name ie My+File.pdf

How can display image in the browser without downloading?

I saved an image in MYSQL database as BLOB format. When I try to fetch that image, the web browser asks me to download it.
I would ask how can I display the image directly in the browser.
The following is the code i used:
<?php
//$id = $_GET['id'];
include_once 'D_B.php';// Connect to server and select database.
$query = "SELECT `name`, `type`, `size`, `content` FROM `upload` WHERE `id`='1'";
$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;
?>
Remove the Content-Disposition header: header("Content-Disposition: attachment; filename=$name"); and it should work fine.

Categories