file download not working php - php

I need your suggestions on this issue..
my intention is, i have a word doc uploaded in db as BLOB content... and i have it displayed as a link in my page..
when i click on the link, the word doc should be downloaded...
i get the below error msg, upon clicking the link..
Warning: Cannot modify header information - headers already sent by (output started at /home/stthohuu/public_html/sp/archive_newsletter.php:8) in
see code below...
</head>
<body>
<?php
//database connection
$con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
//select database
$db = mysql_select_db('stthohuu_church', $con);
$query = "SELECT id, name FROM newsletter order by id desc";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "No files found in DB<br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="archive_newsletter.php?id=<?php echo urlencode($id);?>"
><?php echo urlencode($name);?></a> <br>
<?php
}
}
mysql_close();
?>
</body>
</html>
<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database
$con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
$db = mysql_select_db('stthohuu_church', $con);
$id = $_GET['id'];
$query = "SELECT name, type, size, content " .
"FROM newsletter WHERE id = '$id'";
$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");
ob_clean();
flush();
echo $content;
mysql_close();
exit;
}
?>
this also works in Xampp but NOT in web server :(

header() must be before any output.
Also about - you need to start the output buffering(ob_start()) before you call ob_clean().
<?php
if(isset($_GET['id']))
{
ob_start();
// if id is set then get the file with the id from database
$con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
$db = #mysql_select_db('stthohuu_church', $con);
$id = $_GET['id'];
$query = "SELECT name, type, size, content " .
"FROM newsletter WHERE id = '$id'";
$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");
ob_clean();
echo $content;
mysql_close();
exit;
}
?>
</head>
<body>
<?php
//database connection
$con = mysql_connect('localhost', 'abc', 'abc') or die(mysql_error());
//select database
$db = mysql_select_db('stthohuu_church', $con);
$query = "SELECT id, name FROM newsletter order by id desc";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "No files found in DB<br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="archive_newsletter.php?id=<?php echo urlencode($id);?>"
><?php echo urlencode($name);?></a> <br>
<?php
}
}
mysql_close();
?>
</body>
</html>

Related

I want return JSON type but PHP return nothing

<<? php
//PHP Version 5.2.17
header('Content-Type: application/json');
$connect = mysql_connect("localhost", "ID", "password") or die ("fail");
mysql_query("set session character_set_connection=utf8;");
mysql_query("set session character_set_results=utf8;");
mysql_query("set session character_set_client=utf8;");
$sql = "SELECT * FROM table";
$sql_result = mysql_query($sql, $connect);
$arr = array();
while($row = mysql_fetch_array($sql_result)){
$arr[] = $row;
}
echo (urldecode(json_encode($arr)));
mysql_close($connect);
?>
I want to return type JSON.
But this code returns only
<[]
When I do like this
while($row = mysql_fetch_array($sql_result)){
echo 'Firstname : ' .$row['Firstname '];
echo ' Lastname : ' .$row['Lastname'];
echo '<br/>';
}
echo $sql_result;
This code works normally.
I don't know why my code returns []
my php version is 5.2.17
Thank you in advance.

Download doc from MySQL using PHP

for some reason after I click the url to download, it happen to view the file on my browser which results it massive symbols and random letters. Is there any way to get it to download rather than viewing it on browser? I'm really new to programming, you guidance would really be helpful. Thank you.
<?php
mysql_connect("localhost","root","");
mysql_select_db("tuitioncentre");
if(isset($_GET['id'])) { // if id is set then get the file with the id from database
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM files WHERE id = $id";
$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;
}
?>
<?php
$query = "SELECT id, name FROM files";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<?php echo $name; ?>
<?php
}
}
?>
This is example of the output.
https://i.stack.imgur.com/Cx6XR.png

Display Blob(pdf) with Webview

Now my PHP can display Blob from MySQL on web browser with http://localhost/download.php?id=1
How to display this with webview on android?
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
$sql = "select * from loancontract where id = '$id'";
require_once('db_connect.php');
$r = mysqli_query($con,$sql);
$result = mysqli_fetch_array($r);
header('content-type: application/pdf');
echo base64_decode($result['data']);
mysqli_close($con);
} else {
echo "Error";
}
?>

php script does not show all images stored in a table

in mysql Datbase there is images stored using php Script (image got from a form.html/POST method) let's cal them (phpImages). and there is others stored using android application ( by converting Bitmap to String and using StringBuilder ). let's call them (androidImages).
with this php script i can load and display phpImages, but i cannot display androidImages.
<?php
$con = mysqli_connect("localhost","root","","othmane") or die(mysqli_error($con));
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
$sql = "SELECT image FROM images WHERE id = '$id'";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));;
$result=mysqli_fetch_array($r);
header('Content-Type:image/jpeg');
echo ( $result['image']);
mysqli_close($con);
}
?>
with this php script i can load androidImages, but i cannot load phpImages :
<?php
$con = mysqli_connect("localhost","root","","othmane") or die(mysqli_error($con));
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
$sql = "SELECT image FROM images WHERE id = '$id'";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));;
$result=mysqli_fetch_array($r);
header('Content-Type:image/jpeg');
echo base64_decode( $result['image'] );
mysqli_close($con);
}
?>
i wan't a php script that could display the both. because i want to load all images in a ListView of an android Apps.
**This is php script relied to android Application : **
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
$con=mysqli_connect("localhost","root","","othmane")or die(mysqli_error($con));
$sql = "INSERT INTO images (image,image_type) VALUES (?,'android')";
$stmt = mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt,"s",$image);
mysqli_stmt_execute($stmt);
$check = mysqli_stmt_affected_rows($stmt);
if($check == 1){
echo "Image Uploaded Successfully";
}else{
echo "Error Uploading Image";
}
mysqli_close($con);
}else{
echo "Error";
}
?>
this is php Script relied with Form.html post method :
<?php
echo ini_get( 'file_uploads' );
if(!isset($_POST['submit'])){
echo '<p>Please Select Image to Upload</p>';
}
else
{
try {
upload();
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
}
function upload(){
$imgfp = fopen($_FILES['photo']['tmp_name'], 'rb');
print_r($_FILES);
$dbh = new PDO("mysql:host=localhost;dbname=othmane", 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("INSERT INTO images (image,image_type) VALUES (?,'php')");
$stmt->bindParam(1, $imgfp, PDO::PARAM_LOB);
$stmt->execute();
}
?>
Add a column called image_type in your table and pass one of the following values to determine what the source of the image is upon uploading: phpImage or androidImage
So you can do:
<?php
$con = mysqli_connect("localhost","root","","othmane") or die(mysqli_error($con));
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
$id = $_GET['id'];
$sql = "SELECT image, image_type FROM images WHERE id = '$id'";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));
$result = mysqli_fetch_array($r);
header('Content-Type: image/jpeg');
if ($result['image_type'] == 'phpImage') {
echo ( $result['image']);
} else if ($result['image_type'] == 'androidImage') {
echo base64_decode( $result['image'] );
}
mysqli_close($con);
}
?>

How to load image from the database in html?

This is my code.
<img src="getimage.php?id=1" alt="Delicious World" />
This is getimage.php
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("db_cupcake");
$sql = "SELECT image FROM item WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['0'];
?>
I have directly stored the image in the database. I want to load those images in the webpage. Help me out.
First things first . If you are passing variables in the mysql query , then use quotations .Only then mysql will execute the query .
your query :- $sql = "SELECT image FROM item WHERE id=$id";
Instead use this query :- $sql = "SELECT image FROM item WHERE id='$id'";
And to display the image you can use
echo $row['image'];
You need to echo $row[image]; not echo $row[0];
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "", "db_cupcake");
$result = mysql_query("SELECT image FROM item WHERE id=$id");
$row = mysql_fetch_assoc($result);
header("Content-type: image/jpeg");
echo $row['image'];
mysql_close($link);
?>

Categories