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) );
Related
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!!
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.
The query returns 71 rows but only one image is downloaded. All image files are jpegs with 2 MB maximum file size; most are less than 1 MB. The fsize lines are commented out because they fail to return on the jpg image file.
I have no trouble opening the one image downloaded so I'm doing something right. How can I get the other 70 images? I'm not deep in PHP but I get done what I need to get done. Here's my code:
<?php
$query = "SELECT imagefilename FROM imageinfo WHERE accepted = 1 ";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result))
{
$imagefilename = $row[0];
$sourcefile = 'http://domain.com/images/'.$imagefilename;
if ($fd = fopen ($fullPath, "r"))
{
//$fsize = filesize($sourcefile);
// if IE, otherwise Content-Disposition ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Content-type: image/jpg");
header("Content-Disposition: attachment; filename=$imagefilename");
//header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd))
{
$buffer = fread($fd, 2097152);
echo $buffer;
}
}
fclose ($fd);
} //end while
?>
Patching together several postings, here is the working code I ended with.
//This puts the selected images in a zip file.
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$query = "SELECT imagefilename FROM imagetable WHERE accepted = 1 ";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result) == 0)
{
//Send message.
}
// common vars
$file_path = 'path_to_image_file';
$zipname = 'images.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipARCHIVE::CREATE );
while ($row = mysqli_fetch_array($result))
{
$filename = $row[0];
$zip->addFile($file_path.$filename,$filename);
}
$zip->close();
if (!file_exists($zipname))
{
//Send message
}
//This downloads the zip file.
//zip headers
if (headers_sent())
{
echo 'HTTP header already sent';
}
else
{
if (!is_readable($zipname))
{
header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
echo 'File not readable';
}
else
{
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: ".filesize($zipname));
header("Content-Disposition: attachment; filename=\"".basename($zipname)."\"");
header("Pragma: no-cache");
header("Expires: 0");
readfile($zipname);
exit;
}
}
?>
I am using directory path that i save in database. how can i download from path. my code is.
if(isset($_POST['download'])) {
$id = $_GET['id'];
$query = "SELECT url_video FROM website WHERE id = '$id'";
$result = mysql_query($query); $rowss = mysql_fetch_assoc($query);
$name2 = $rowss['url_video'];
header("Accept-Ranges: bytes");
header("Keep-Alive: timeout=15, max=100");
header('Content-Disposition: attachment; filename='.$name2);
header("Content-type: video/mp4");
//header("Content-Transfer-Encoding: binary");
header( "Content-Description: File Transfer");
exit;
}
?>
You need to output/echo the content of the file after you set the download headers.
Also the following
header('Content-Disposition: attachment; filename='.$name2);
should be
header('Content-Disposition: attachment; filename='.$file_name_not_full_path);
or
header('Content-Disposition: attachment; filename=downloadedvideo.mpg');
Full code (i assume you have a column named video_path that stores the path to the file on the server:
if(isset($_POST['download'])) {
$id = $_GET['id'];
query = "SELECT url_video, video_path FROM website WHERE id = '$id'";
$result = mysql_query($query);
$rowss = mysql_fetch_assoc($query);
header("Accept-Ranges: bytes");
header("Keep-Alive: timeout=15, max=100");
header('Content-Disposition: attachment; filename='.$rowss['url_video']);
header("Content-type: video/mp4");
header("Content-Transfer-Encoding: binary");
header( "Content-Description: File Transfer");
readfile($rowss['video_path']);
}
New code version after last comments:
if(isset($_POST['download'])) {
$id = $_GET['id'];
query = "SELECT url_video FROM website WHERE id = '$id'";
$result = mysql_query($query);
$rowss = mysql_fetch_assoc($query);
header("Location: ".$rowss['url_video']);
exit();
}
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