PRodgers4284 Posted February 13, 2008 Share Posted February 13, 2008 I am having trouble downloading a word doc from a mysql database, i have the upload file working and going to the databas, just cant same to get the download file working, can anyone please help me or provide some advice as to where im going wrong. My upload code is: <?php session_start(); include("database.php"); if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>New Page 2</title> </head> <body> <form method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> </body> </html> My download code is: <html> <head> <title>Download CV</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php include("database.php"); $query = "SELECT id, name FROM upload"; $result = mysql_query($query) or die('Error, query failed'); if(mysql_num_rows($result) == 0) { echo "Database is empty <br>"; } else { while(list($id, $name) = mysql_fetch_array($result)) { ?> <a href="download.php?id=<?php=$id;?>"><?php=$name;?></a> <br> <?php } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
aschk Posted February 13, 2008 Share Posted February 13, 2008 You're doing addslashes() to the word document file $content. I can only imagine this will screw up the format of the content (seeing as you'll be reading it in binary). Also, what datatype is your "content" field in your database? I hope you made it a BLOB... Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 You're doing addslashes() to the word document file $content. I can only imagine this will screw up the format of the content (seeing as you'll be reading it in binary). Also, what datatype is your "content" field in your database? I hope you made it a BLOB... I have the data type of the content field as a blob Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 aschk when i remove the add slashes the file wont upload, just outputs at error Quote Link to comment Share on other sites More sharing options...
aschk Posted February 13, 2008 Share Posted February 13, 2008 What's the error? Also when they download said file, you need to present it's encoding as an application/ms-word or something similar via a header() call (this will cause the browser to download the file (or display it). As far as I can see all you've provided thus far is a page with a "link" to it via id. What does download.php look like? note: probably not a problem with addslashes after thinking about it. Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 What's the error? Also when they download said file, you need to present it's encoding as an application/ms-word or something similar via a header() call (this will cause the browser to download the file (or display it). As far as I can see all you've provided thus far is a page with a "link" to it via id. What does download.php look like? note: probably not a problem with addslashes after thinking about it. Thanks for the reply, the error when i remove the add slashes is "error, query failed", the file does not upload to the database The download.php displays a blank page, no errors. Quote Link to comment Share on other sites More sharing options...
pdkv2 Posted February 13, 2008 Share Posted February 13, 2008 use this in your "download.php" header('Content-Type: application/ms-word'); Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 use this in your "download.php" header('Content-Type: application/ms-word'); Thanks, does this go at the top of the download code then? Quote Link to comment Share on other sites More sharing options...
pdkv2 Posted February 13, 2008 Share Posted February 13, 2008 in the download.php replace header('Content-Type: image/png'); with the header('Content-Type: application/ms-word'); Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 I have modified the download.php code to this but this no joy: <?php session_start(); include("database.php"); if(isset($_GET['id'])) { $id = $_GET['id']; $query = "SELECT name, type, size, content FROM upload WHERE id = '$id'"; $result = mysql_query($query) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); header("Content-type: $type"); echo $content; exit; } ?> <html> <head> <title>Download CV</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">S </head> <body> <? include("database.php"); $query = "SELECT id, name FROM upload"; $result = mysql_query($query) or die('Error, query failed'); if(mysql_num_rows($result) == 0) { echo "Database is empty <br>"; } else { while(list($id, $name) = mysql_fetch_array($result)) { ?> <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br> <? } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 in the download.php replace header('Content-Type: image/png'); with the header('Content-Type: application/ms-word'); Im confused, i dont have "header('Content-Type: image/png');" in the code, sorry about all the question btw, appreciate your help on this Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 sorry, my mistake , i changed the header type, the code just output an "S" now when i run the download code. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 13, 2008 Share Posted February 13, 2008 Here is the header set you need: <?php header("Content-type: Content-Type: application/msword; charset=ISO-8859-1"); header("Content-Disposition: attachment; filename=\"$name\""); header("Content-length: $size"); ?> note: the content-type is "application/msword" Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 Here is the header set you need: <?php header("Content-type: Content-Type: application/msword; charset=ISO-8859-1"); header("Content-Disposition: attachment; filename=\"$name\""); header("Content-length: $size"); ?> note: the content-type is "application/msword" I have changed the header in the download.php file but it just keeps displaying a blank page with the letter "S" for some reason. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 13, 2008 Share Posted February 13, 2008 Hmm, i've pretty much the same thing (to test) the only difference being that i'm not pulling from the database. Are you sure that the $content coming back it correct (i.e looks like it might be). Do you SQL query and just echo $content (WITHOUT the headers) and see what it looks like. Quote Link to comment Share on other sites More sharing options...
PRodgers4284 Posted February 13, 2008 Author Share Posted February 13, 2008 Hmm, i've pretty much the same thing (to test) the only difference being that i'm not pulling from the database. Are you sure that the $content coming back it correct (i.e looks like it might be). Do you SQL query and just echo $content (WITHOUT the headers) and see what it looks like. Ive checked the $content coming back and it looks correct, ive tried removing the headers and only echo out the $content and it still brings up a blank page with the letter "S", its strange, cant understand why its doin that. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 18, 2008 Share Posted February 18, 2008 Just a follow up on this. I've written by own version of this and it appears to work fine with those headers. Just make sure you are echo'ing or print'ing the content out. I forgot to do that and was wondering while the files were saving as .txt in some instances and contained nothing. LOL, DOH! 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.