Hello,
I have a script that allows user to upload a file to a mysql 5 database. That is working OK. The File size is accurate. However, my download script is for some reason downloading the files without any content - 0KB. The files are OK when I open them directly. I have tried code with .doc, .pdf, .txt and the same result.
Here is my code for viewing the files:
<?php
ini_set('display_errors', E_ALL);
include "open_db.inc";
$sql = "SELECT * FROM tbl_Files ";
$sql .= "ORDER BY filename ASC";
$result = mysql_query($sql, $db);
$rows = mysql_num_rows($result);
echo "<table>\n";
echo " <tr>\n";
echo " <td>ID</td>\n";
echo " <td>Filename</td>\n";
echo " <td>Type</td>\n";
echo " <td>Size</td>\n";
echo " <td>Description</td>\n";
echo " <td> </td>\n";
echo " </tr>\n";
while ($rows = mysql_fetch_object($result))
{
echo " <tr>\n";
echo " <td>$rows->id_files</td>\n";
echo " <td>$rows->filename</td>\n";
echo " <td>$rows->filetype</td>\n";
echo " <td>$rows->filesize</td>\n";
echo " <td>" . stripslashes($rows->description) . "</td>\n";
echo " <td>( <a href='download.php?id_files=$rows->id_files'>Download</a> )</td>\n";
echo " </tr>\n";
}
mysql_free_result($result);
mysql_close($db);
?>
The code for download.php is:
<?php
ini_set('display_errors', E_ALL);
if (isset($_GET['id_files']))
{
$id_files = $_GET['id_files'];
include "open_db.inc";
$sql = "SELECT id_files, bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=$id_files";
$result = @mysql_query($sql, $db);
$dataT = @mysql_result($result, 0, "bin_data");
$name = @mysql_result($result, 0, "filename");
$size = @mysql_result($result, 0, "filesize");
$type = @mysql_result($result, 0, "filetype");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
header("Content-transfer-encoding: binary");
echo $dataT;
}
else
{
echo 'id_files not set';
}
?>
I would appreciate any help.
Thanks,
TMLAH