simbu Posted November 26, 2012 Share Posted November 26, 2012 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.. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 26, 2012 Share Posted November 26, 2012 (edited) 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 November 26, 2012 by AyKay47 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.