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..

Link to comment
Share on other sites

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.

Edited by AyKay47
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.