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();
}
}
?>
Related
I am trying to download the files from database. but it shows "failed to load" message.Uploading is working fine. i can upload pdf, jpg, dox file. but unable to open when i download.IF there are three href links in my file. first is for "upload" second is for "delete". every link download the file from image even other links are for other work but every file calls the downld.php file
<?php echo $name;?>
downld.php
<?php
if (isset($_GET['id']))
{
$project_id = $_GET['id'];
$myConnection= mysqli_connect("localhost","root","", "extra") or die ("could
not connect to mysql");
mysqli_set_charset($myConnection,'utf-8');
global $wpdb;
$sql = "SELECT name, type, size, content " .
"FROM wp_postprojectwin WHERE project_id = $project_id";
$result = mysqli_query($myConnection,$sql) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
mysqli_close($connection);
ob_clean();
flush();
echo $content;
exit;
}
else{
echo "sorry";
}
?>
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();
Recently moved a site over to a new server and now for some reason the download page is no longer downloading the purchased files.
When the download page is reached it is simply blank. I have changed the filename directory to use home/mastetu8 instead of home/mastvoic which was the last directory. Any idea on why the page is blank when trying to download?
Here is the download_process.php file currently running this piece:
<?php ob_start(); ?>
<?php require_once('../Connections/admin.php'); ?>
<?php
$orders_id = $_GET['orderid'];
$id = $_GET['id'];
mysql_select_db($database_admin, $admin);
$query_rsOrdersDetail = "SELECT * FROM orders_detail LEFT JOIN media ON orders_detail.product_id = media.sortorder WHERE orders_detail.orders_id = '$orders_id' AND orders_detail.id = '$id'";
$rsOrdersDetail = mysql_query($query_rsOrdersDetail, $admin) or die(mysql_error());
$row_rsOrdersDetail = mysql_fetch_assoc($rsOrdersDetail);
$totalRows_rsOrdersDetail = mysql_num_rows($rsOrdersDetail);
// CHECK DOWNLOAD COUNT
if ($row_rsOrdersDetail['downloadcount'] <= 3) {
$filename_hires = '/home/mastetu8/public_html'.$row_rsOrdersDetail['filename_hires'];
if (file_exists($filename_hires)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename_hires));
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_hires));
ob_clean();
flush();
readfile($filename_hires);
// UPDATE DOWNLOAD COUNTER
$downloadcount_new = $row_rsOrdersDetail['downloadcount'] + 1;
mysql_select_db($database_admin, $admin);
mysql_query("UPDATE orders_detail SET downloadcount='$downloadcount_new' WHERE id = '$id' AND orders_id = '$orders_id'", $admin) or die(mysql_error());
}
}
?>
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();
}