Database > MySQL database

Storing word Document file to mysql database

(1/5) > >>

pankaj.durve1990:
I have to store complete Word Document(.docx)  file into MySql database by using Php code.

wilson382:
ITs a good Idea t store files in database for security BUT

i have script that do just that but when i download a file they are unreadable for example if i download a winrar i get the corupted file by wirar

wilson382:
let's make it work for both of us

upload.php

--- PHP Code: ---
<?php
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') or die("unable to open file");
$content = fread($fp, filesize($tmpName))  or die("unable to read file");
$content = addslashes($content)  or die("unable to add slahshes");
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

$query = "INSERT INTO files (name, size, type, content ) VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

$result = mysql_query($query) or die(mysql_error()); 

mysql_close($conn) or die(MYSQL_ERROR());

if ($result) 
{
$content;
echo "<br>File $fileName uploaded<br>";
}

?>

<html>
<head>
</head>
<body>
<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>

--- End code ---


download.php

--- PHP Code: ---
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'database.php';

$query = "SELECT id, name FROM files";
$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))
{

echo "<a href='download.php?id={$id}'>$name</a><br>";

}
}
echo $id;

if(isset($_GET['id'])) 
{

$id= $_GET['id'];

$query = "SELECT name, type, size, content FROM files WHERE ID = '$id'";

$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");
//===============================
    header("Pragma: public"); // required 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private",false); // required for certain browsers 
    header("Content-Type: $type"); 
    header("Content-Disposition: attachment; filename=$name"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".$size);
echo $content;

mysql_close($conn);
exit;
}

?>


</body>
</html>

--- End code ---


I have looked over this script over and over and irs works fine but lol the files are not usable after u download them

wilson382:
I know what is wrong but I do not know how to fix it

I uploaded a text files contain plain text "this is just a test!!!!!!!!"

when i downloaded, the file contains is the following


--- Code: ---<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<a href='download.php?id=1'>GPU_Rebate.pdf</a><br><a href='download.php?id=2'>GPU_Rebate.pdf</a><br><a href='download.php?id=3'>PSU_Rebate.pdf</a><br><a href='download.php?id=4'>Rebates.zip</a><br><a href='download.php?id=5'>Rebates.zip</a><br><a href='download.php?id=6'>IMAG0156.jpg</a><br><a href='download.php?id=7'>Auto Typer.exe</a><br><a href='download.php?id=8'>Auto Typer.vshost.exe.manifest</a><br><a href='download.php?id=9'>Auto Typer.vshost.exe</a><br><a href='download.php?id=10'>Auto Typer.vshost.exe.manifest</a><br><a href='download.php?id=11'>klktudice.kdbx</a><br><a href='download.php?id=12'>IMAG0158.jpg</a><br><a href='download.php?id=13'>Settings.txt</a><br><a href='download.php?id=14'>help to remember forgotten password.txt</a><br><a href='download.php?id=15'>we3r4.txt</a><br>this is just a test!!!!!!!!

--- End code ---

and i add the end "this is just a test!!!!!!!!"

ErnieAlex:
Tossing in further info...

   If you load a file into a variable, it is loaded with all kinds of issues.  It could contain code that when
retrieved could activate and run a malicious program.  So, if someone is loading and storing a word doc,
make sure it is virus checked after it is retrieved.  Or, at the least, make sure you tell the retrieving user
that THEY should virus check it.

   Also, when up load a file into a variable to store in a database, you should do so as a binary format.
In this way you can retrieve as a binary format and then save it in it's original format.  If you try to use
other encoding to add or removing formatting, it will never be the same file.  It must be done in binary
to make sure that nothing has been altered.

Hope that makes sense...  Good luck and let us know either way if it is solved...

Navigation

[0] Message Index

[#] Next page

Go to full version