Hi,
Would it be possible for someone to test the following code. I am using the code to upload and download binary files, specifically MS word documents. The uploading of word documents seems to be working, well at least the database is showing that they have been stored, although I do not know if the files were corrupted during the upload process. However, when I try to download the word docs all I get are a few illegible characters. I do not know what is causing this. I have used different character sets on my database during upload and download, but nothing is making any difference to the output. I wonder if someone might test this to see if it works for them. That way I’ll know if the problem is unique to me. I would be very grateful for your help.
//Table created in database to store binary file
CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);
//HTML file - Get Word doc from user (stored on web server)
<html>
<body bgcolor="#FFFFFF">
<form enctype="multipart/form-data" name="Get File From User.html" action="X.php" method="post">
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#111111"
width="85%">
<tr> <td width="100%" bgcolor="#FF9900" height="22" colspan="2">
<p style="margin-left: 10"><b><font face="Verdana" size="2"
color="#FFFFFF"> Upload Your MS Word Doc</font></b></td> </tr>
<tr> <td width="100%" bgcolor="#FFE3BB" colspan="2">
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2">
<br>Please upload your file for saving in our database. This file must be a
MS Word doc. Once you have selected click on the
"Upload" button below. <br>
</font> </td> </tr>
<tr> <td width="15%" bgcolor="#FFE3BB">
<p style="margin-left: 10"><font face="Verdana" size="2">
File Location:</font> </td>
<td width="85%" bgcolor="#FFE3BB">
<font face="Verdana" size="2">
<input type="file" name="fileUpload" id="file">
</font> </td> </tr>
<tr> <td width="33%" bgcolor="#FFE3BB">
<p style="margin-left: 10"><font face="Verdana" size="2"> <br>
<br> </font> </td>
<td width="67%" bgcolor="#FFE3BB">
<font face="Verdana" size="2">
<input type="submit" value="Upload" name="cmdSubmit">
</font> </td> </tr> </table> </form>
</body> </html>
//PHP file - Code to upload binary file (X.php) (stored on web server)
<?php
if(isset($_POST['cmdSubmit']) && $_FILES['fileUpload']['size'] > 0)
{ $fileType = $_FILES['fileUpload']['type'];
if($fileType == "application/msword") //only accept MS Word docs
{ $fileName = $_FILES['fileUpload']['name'];
$fileSize = $_FILES['fileUpload']['size'];
$tmpName = $_FILES['fileUpload']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{ $fileName = addslashes($fileName);
}
$con = mysql_connect("………..", "…………","……………");
mysql_select_db("…………", $con);
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
mysql_close($con);
echo "<br>File $fileName uploaded<br>";
}
else
{ echo "<br>Must be a MS Word Doc<br>";
}
}
?>
//PHP file - Code to download binary file (stored on web server)
<?php
$con = mysql_connect("........","......","........");
mysql_select_db(".......", $con);
$query = "SELECT name, type, size, content " .
"FROM upload WHERE id = 1";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
mysql_close($con);
exit;
?>