i have blob images that are stored in the database I want to download it to my device.
the image will be download (as ***.jpg) but it is corrupted .
this is my download.php code
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "text_blob1";
$con= new mysqli($servername, $dbusername, $dbpassword, $dbname);
$id=$_GET["id"];
$sql = "select * from table1 where id=$id "; // 1
$res = $con->query($sql);
while($row = $res->fetch_assoc())
{
$name = $row['name'];
echo "<br>";
$size = $row['size'];
echo "<br>";
$type = $row['extension'];
echo "<br>";
$image = $row['image'];
}
header("Content-type: ".$type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".$size);
echo $image;
exit($image);
?>
thanx <3
Everything you output to the page is considered a file contents. I suppose you don't want "<br>" to be in the contents of your file.
Second point - do not do any output before setting headers.
Third point - exit('string') outputs 'string', so you output content of your file twice: with echo and with exit.
So, your code should look like:
$id=$_GET["id"];
$sql = "select * from table1 where id=$id "; // 1
$res = $con->query($sql);
while($row = $res->fetch_assoc())
{
$name = $row['name'];
$size = $row['size'];
$type = $row['extension'];
$image = $row['image'];
}
header("Content-type: ".$type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".$size);
echo $image;
exit();
Related
when download image image is can't open .it just 4kb file.please help. image type is in blob.its downloading file.but can't visible.
<?php
if(isset($_GET['task_id'])){
$id = $_GET['task_id'];
$sql = "SELECT * FROM workload WHERE TID = $id ";
$info = $obj_admin->manage_all_info($sql);
$num_row = $info->rowCount();
if($num_row>0){
$i=0;
}
while( $row = $info->fetch(PDO::FETCH_ASSOC) ){
$image = 'images_uploaded/'. $row['imageA'];
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="table_with_image_image'.$id.'.jpg"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".strlen($image));
echo $image;
exit();
}
}
?>
My code was working and I don't see issues with it. I tested all the MySQL statements in SQL and everything is fine there.
Everytime it fetches the password variable now it only returns Bad Password as if m_Pass is empty.
Anyone see anything wrong with those statements I don't?
I am using this as a one time password file transfer tool that you have to specify the filename and the password for accessing it.
Then it checks the SQL Database that contains the following format:
id | m_File | m_Pass
0 | Test.txt | testpass
Any help would be appreciated.
Index.php:
<?php
echo <<<EOF
<form method="post" action="Index.php">
File:<input type="text" name="txtFilename"><br>
Password:<input type="text" name="txtPassword">
<input type="submit" value="Download" name="submit">
</form>
EOF;
function doProcess()
{
$servername = "localhost";
$username = "<username>";
$password = "<Password>";
$dbname = "<dbname>";
$filename = $_POST["txtFilename"];
$pass = $_POST["txtPassword"];
echo $filename . "<br>";
if ($filename = ""){
return;
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully" . "<br>";
$sql = "SELECT id, m_Pass FROM Main WHERE m_File = '$filename'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$m_id = $row["id"];
echo $m_id . "<br>";
if ($row["m_Pass"] == $pass){
echo "Downloading.<br>";
//Download File
$filename = utf8_decode("<Path>" . $filename);
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
$sql = "delete from Main where id = '$m_id'";
$result = mysqli_query($conn, $sql);
}
} else {
echo $pass . " | '" . $row["m_Pass"] . "'<br>";
echo "Bad Password.<br>";
}
echo "Done.<br>";
// Close connection
$conn->close();
}
if(isset($_POST['submit']))
{
doProcess();
}
?>
Got it... Turns out the SQL query command wasnt appending the $filename because of the block:
if ($filename = ""){
return;
}
And I see why... I am assigning the value not comparing lol.
I am getting an error to fetch the pdf file from the database.Below mentioned is my code,please review my code and give me your valuable suggestion.And it's showing output as failed to open the document.Please help me.
<?php
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'upload';
// Connect to Database
$connection = mysql_connect($server, $user, $pass) or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());
$id = intval($_GET['id']);
$file= 'SELECT `name`,`size`, `created`,`data` FROM `upload`';
$result = mysql_query($file);
if($d = mysql_fetch_array($result))
{
$file = $d['name'];
header('Content-type: application/pdf');
header("Content-Disposition: inline; name=".$row['name']);
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . size($file));
header('Accept-Ranges: bytes');
header("Location: $file");
#readfile($file);
}
else{
echo'No file with the specified ID exists';
}
?>
One issue that I see is the single "=" in your if statement. '=' is for assignment and '==' is for comparison.
Try changing
if($d = mysql_fetch_array($result))
to
if($d == mysql_fetch_array($result))
EDIT: However, I don't think that will quite work either. I would try
$d = mysql_fetch_array($result);
if ($d === true)
if($result) {
// Make sure the result is valid
if($result->num_rows == 1) {
$row = mysqli_fetch_assoc($result);
header('Content-type: application/pdf');
header("Content-Disposition: inline; name=".$row['name']);
header('Content-Transfer-Encoding: binary');
header("Content-Length: ". $row['size']);
header('Accept-Ranges: bytes');
// Print data
#readfile($row['data']);
echo $row['data'];
}
else {
echo 'Error! No image exists with that ID.';
}
When using computer files are downloaded normally.
Problem start on android browsers.
When we download a file from android default browser, the file is downloaded but it is of 0.00 bytes, so not technically present there(corrupt file).
When we download file from any third party application like or opera it gives error that "file could not be downloaded." not the header error. And files could be downloaded via UC Browser and Chrome and Firefox. But still gives the error in default browser.
Here is the code I am using:
<?php
require 'php/db.php';
if(isset($_POST['file_id'])&&!empty($_POST['file_id'])){
download_file($_POST['file_id']);
}
function download_file($id){
global $con;
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id";
$result = mysqli_query($con,$file);
$row = mysqli_fetch_assoc($result);
$name = $row['file_name'];
$title = $row['file_title'];
$size = $row['file_size'];
$ext = strtoupper(ext($name)); // Function defined bellow
$down = $row['down'];
$newname = $title.'.'.$ext;
$olddir = "files/".$name;
$down++;
if(is_file($olddir)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down);
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($olddir)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$newname.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size); // provide file size
header('Connection: close');
readfile($olddir);
exit();
}else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded");
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
We give file id to this function and it downloads the file on PC.
Can anyone tell me how to remove the error? So that users can download files from their android phones too.
Probably $size == 0;
$size = $row['file_size'];
...
$olddir = "files/".$name;
change to
$olddir = "files/".$name;
$size = filesize($olddir);
And change
else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded");
to
else header("Location: index.php?msg=Sorry!+File+could+not+be+found+on+server: " . $olddir);
I got the solution.
Mainly I changed two things:
Used GET method instead of Post Method.
Used Flush and ob_clean functions to clear the buffer.
New code for downfile.php is like this:
<?php
require '../php/db.php';
ob_start();
if(isset($_GET['file_id'])&&!empty($_GET['file_id'])){
download_file($_GET['file_id']);
}else die("There was an error in downloading file. Please try again later.");
function download_file($id){
global $con;
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id";
$result = mysqli_query($con,$file);
$row = mysqli_fetch_assoc($result);
$name = $row['file_name'];
$title = $row['file_title'];
$ext = ext($name);
$down = $row['down'];
$newname = $title.'.'.$ext;
$size = $row['file_size'];
$down++;
if(is_file($name)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$newname.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.$size);
ob_clean();
flush();
readfile($name);
exit;
}else header("Location: index.php?msg=Sorry!+File+could+not+found!");
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
?>
Through this code I am able to download files in android browsers too.
Hope this may help. :)
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();
}