Jump to content

How To Download A Doc File From Database In Phpmyadmin?


simbu

Recommended Posts

Hello,

 

I want to download a document file from database in phpmyadmin.

 

Here is my codings...

 

 

<?php

if(isset($_GET['id']))

{

// if id is set then get the file with the id from database

 

mysql_connect("localhost","root","");

mysql_select_db("sample");

 

$id = $_GET['id'];

$query = "SELECT file FROM testing WHERE id='$id'";

 

$result = mysql_query($query);

list($name) = mysql_fetch_array($result);

 

header("Content-length: ''");

header("Content-type: application/msword");

header("Content-Disposition: attachment; file='$name'");

echo "";

 

 

exit;

}

 

?>

 

If i am download , i am getting only empty document .

 

If any one knows let me know..

You are receiving an empty document because you are outputting an empty string.

Force a download using readfile

Ex:

 

if(file_exists($name))
{
 header("Content-length: " . filesize($name));
 header("Content-type: application/msword");
 header("Content-Disposition: attachment; filename=" . basename($name));
 ob_clean();
 flush();
 readfile($name);
 exit;
}
else
{
 //file cannot be found
}

 

As a side note, your script is vulnerable to SQL injection. Validate $_GET['id'] before using it in a query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.