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
Related
Why can't my php display the image?
<?php
include("sql.php");
//$sql = "SELECT * FROM filesdb WHERE fileid = 5";
$id = $_GET['id'];
// do some validation here to ensure id is safe
$sql = "SELECT * FROM filesdb WHERE fileid =$id";
echo $sql."<br>";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//header('content-type: image/jpeg');
echo "<br>Fileid:".$row['fileid'];
echo "<br>FileName:".$row['filename'];
header("Content-Type: image/jpeg");
echo "<br>".$row['dbforfile'];
}
it can display fileid and filename, but not dbfofile, which is a BLOB.
If you're downloading the file contents, you must not put HTML elements around it.
You can also only send one image at a time, so there's no point in looping.
<?php
include("sql.php");
//$sql = "SELECT * FROM filesdb WHERE fileid = 5";
$id = $_GET['id'];
// do some validation here to ensure id is safe
$sql = "SELECT dbforfile FROM filesdb WHERE fileid =$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
header('content-type: image/jpeg');
$row = $result->fetch_assoc();
echo $row['dbforfile'];
}
You are only doing one image, so you don't need a loop. But if you want to do multiples; you need to use an img tag and call another PHP script as if it were an image in the src and have that script output the image (with headers). Or you can use base64 encoded data as the image source in the tag:
while($row = $result->fetch_assoc()) {
echo "<br>Fileid:".$row['fileid'];
echo "<br>FileName:".$row['filename'];
echo "<br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['dbforfile']).'"/>';
}
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";
}
?>
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);
}
?>
I'm trying to download a file from my database and I'm getting back a php file as an output. But when I use var_dump, I'm able to get the output shown in Figure 1
and Figure 2.
I want to be able to download the file as its actual file type. (i.e. as a pdf) Can anyone please help me with this? Thanks in advance :)
Here is my download code:
try {
//if id is set then get file from database
if (isset($_GET['id'])) {
$id = $_GET['id'];
//pump id into function getDBFiles to pull file with matching id
$Download->getDBFiles($id);
//get array containing file details from function getDBFiles
$getFiles = $Download->getMessages();
//var_dump($getFiles);
$size = $getFiles['size'];
$type = $getFiles['type'];
$name = $getFiles['name'];
$content = $getFiles['content'];
header("Content-length: ".$size.'"');
header("Content-type: application/vnd.openxmlformats");
error_reporting(0);
header('Content-Disposition: attachment; filename="' . $name . '"');
echo $content;
exit;
}
//execute retrieval of files from database
$Download-> showDBFiles();
//pass results to output array
$output = $Download->getMessages();
//var_dump($output);
} catch (Exception $e) {
$result[] = $e->getMessages();
}
This is where the functions dealing with the database are called:
protected $messages = array();
public function showDBFiles() {
global $database;
$sql = "SELECT resume_id, resume_title FROM ".self::$table_name;
$result = $database->query($sql);
if(mysqli_num_rows($result)==0){
$this->messages[] = "Database is empty";
}else{
while(list($id, $name) = mysqli_fetch_array($result)){
$this->messages[] = array('id'=>$id, 'name'=>$name);
}
}
}
public function getDBFiles($id) {
global $database;
$sql = "SELECT resume_title, file_type, file_size, resume_data FROM ".self::$table_name." WHERE resume_id = $id";
$result = $database->query($sql);
list($name, $type, $size, $content) = mysqli_fetch_array($result);
$this->messages[] = array('name'=>$name, 'type'=>$type, 'size'=>$size, 'content'=>$content);
}
public function getMessages()
{
return $this->messages;
}
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>