Jump to content

Download word doc form a mysql database


PRodgers4284

Recommended Posts

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>




Link to comment
https://forums.phpfreaks.com/topic/90885-download-word-doc-form-a-mysql-database/
Share on other sites

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

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

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.

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.

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>


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

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"

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.

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.

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.

 

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!

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.