downloading Blob files with a link or a button - php

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.

Related

Downloading image stored as a BLOB does not work

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!!

Failed to open pdf file [duplicate]

This question already has answers here:
Chrome has "Failed to load PDF document" error message on inline PDFs
(7 answers)
Closed 5 years ago.
My code is working fine. My files are downloading also but when I open one file then it is not opening giving an error "Error
Failed to load PDF document."
<?php
$pno = $_GET['pno'];
$sql = "SELECT file FROM tenders WHERE Tno = $id";
$file = "data/" . $mysql_row['file '];
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
readfile($file);
?>
$file = "path_to_file";
$fp = fopen($file, "r") ;
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$myFileName."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' . filesize($file));
ob_clean();
flush();
while (!feof($fp)) {
$buff = fread($fp, 1024);
print $buff;
}
exit;
I would recommend to always check if the file exists. If it doesn't readfile() would eventually put an error inside your pdf-file which may cause the problem. Try it like this:
$pno = $_GET['pno'];
$sql = "SELECT file FROM tenders WHERE Tno = $id";
$file = "data/" . $mysql_row['file '];
if(file_exists($file)){
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
readfile($file);
} else {
echo "File does not exist!";
}
Also there is no declaration of the $id variable. Could it be that $pno should be changed to $id?

Php Image save as dialog box

I wanted to open a save image dialog box when I click on an image. I managed to open the same but when saved, it does not save open saved image as the content of image is not saved somehow.
PHP code:
$imageName = $_GET['i'];
$imageName = $imageName . '-HR.jpg';
header ("Content-Type: application/download");
header ("Content-Disposition: attachment; filename=$imageName");
header("Content-Length: " . filesize("$imageName"));
$fp = fopen("$imageName", "r");
fpassthru($fp);
The passing URL is something like:
mydomain/download_image.php?c=atherothrombosis&i=embolus-carotid-artery-illustration
Please suggest solution. Thanks.
add this header also
header("Content-Type: application/force-download");
I managed to do so by using below code:
<?php
$imageName = $_GET['i'];$imageName = $imageName . '-HR.jpg';
$imageCatName = $_GET['c'];
$imageCatName = ucwords($imageCatName);
$file_path = $docRoot . '/static/media/images/content/image_library/'.$imageCatName . '/'. $imageName;
if(file_exists($file_path)) {
header("Content-disposition: attachment; filename={$imageName}");
header('Content-type: application/octet-stream');
readfile($file_path);
}else {
echo "Sorry, the file does not exist!";
}
?>
still thanks a lot for your support. :)

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.

I want to display an image instead of downloading it (Image in database blob)

I want display an image instead of downloading it.
I have image in my database table, column as BLOB.
This snippet downloads the image, but I want to display it instead:
$query = "SELECT * FROM upload";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$content = $row['content'];
$size = $row['size'];
$type = $row['type'];
header("Content-length: $size");
header("Content-type: $type");
// The following headers make the image download, but I don't want it to
// download, I want to show the image. What should I do?
// header("Content-Disposition: attachment; filename=$name");
echo $content;
The opposite content-disposition of attachment is inline. Try this:
header("Content-Disposition: inline; filename=$name");
if you want to use this more dynamically make a script of your original code and call it like this:
<img src="image.php?imageid=$myImageID" />
and your script is:
$myImageID = $_GET["myImageID"];
$query = "SELECT * FROM upload where id='"+$myImageID+"'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$content = $row['content'];
$size = $row['size'];
$type = $row['type'];
header("Content-length: $size");
header("Content-type: $type");
//header("Content-Disposition: attachment; filename=$name");---> this headers make system to download , but i dont want to download, i want to show image, what should i do ,
echo $content; ?>"
you need to be sure nothing else than your headers and the image content is sent to the client. maybe you want to do an exit after echo $content; this is not a best practice and also does not ensure that nothing else has been sent before you output the image content, but it should do the job.
You can display the image instead of downloading it with this code:
Click to download

Categories