3 Replies - 2143 Views - Last Post: 11 May 2009 - 08:49 AM Rate Topic: -----

#1 raydona  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-May 09

Cannot download binary file from database

Posted 09 May 2009 - 05:49 PM

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 
    &quot;Upload&quot; button below.&nbsp; &nbsp;<br>&nbsp;
</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> &nbsp;</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;
?>


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 09 May 2009 - 09:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot download binary file from database

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,139
  • Joined: 08-August 08

Re: Cannot download binary file from database

Posted 09 May 2009 - 06:26 PM

Why would you store the file in a database? That's asking for problems. Treat it like you would a graphic. Save the file in a directory, and store its path, who uploaded it, size, etc., in the database.
Was This Post Helpful? 0
  • +
  • -

#3 raydona  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-May 09

Re: Cannot download binary file from database

Posted 11 May 2009 - 08:20 AM

View PostCTphpnwb, on 9 May, 2009 - 05:26 PM, said:

Why would you store the file in a database? That's asking for problems. Treat it like you would a graphic. Save the file in a directory, and store its path, who uploaded it, size, etc., in the database.

Hi,
Many thanks for your reply. I've taken up your suggestion and created a folder on my host's server to store the word docs. I've written the php code shown below to upload the files to the newly created directory. Unfortunately, as I am new to all this I don't think my code is right, so the files are not being sent to the directory. Also the filepath in the database is just showing the file name and not the url.
<?php
$uploadDir = realpath("http://www.???.com/NewDirectory/");
if(isset($_POST['Submit']))
{
$fileName = $_FILES['fileUpload']['name'];
$tmpName = $_FILES['fileUpload']['tmp_name'];
$fileSize = $_FILES['fileUpload']['size'];
$fileType = $_FILES['fileUpload']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
MYSQL_CONNECT("....","....","....");
mysql_select_db("....");
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
mysql_close($con);
echo "<br>Files uploaded<br>";
}
?>
I would be very grateful if you could show me where to make the necessary changes. I've searched the internet but I cannot find a simple answer. Most of the examples are too technical for me.
Ray
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,139
  • Joined: 08-August 08

Re: Cannot download binary file from database

Posted 11 May 2009 - 08:49 AM

You're missing a slash here:
$filePath = $uploadDir ."/". $fileName;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1