Please help. I'm doing this tutorial from http://www.php-mysql-tutorial.com/upload-to-file-server.php where the files are not stored in the datebase but moved to a specified folder and renamed (to avoid overwriting files with the same name).
The upload script seems to work exactly as intended. I can go into the "specified folder" in my file manager in my web hosting control panel and the files open with their associated application (i.e. .xls files open with MS Excel). However, when I click on the links generated by the download2.php script it outputs the binary garbage to the browser window rather than opening the file with the associated application. If I right-click to download, the file comes up as "download2.php" and the file type as html.
I also get the "can't modify header" errors if the leave the header calls in the script. The header calls are before any HTML tags, so why do I still get those errors. I don't get the errors, if I take them out of the script, but the files still output as binary.
Can someone please help? Here's download2.php code:
<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
include 'library/config.php';
include 'library/opendb.php';
$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
readfile($filePath);
include 'library/closedb.php';
exit;
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include 'library/config.php';
include 'library/opendb.php';
$query = "SELECT id, name FROM upload2";
$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="download2.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?php
}
}
include 'library/closedb.php';
?>
</body>
</html>