Download in PHP
Posted by:
Ng Py
Date: April 07, 2006 02:46AM
Hi,
I am newbie in PHP and currently working for an e document management systems. I faced some problem in the download coding that i did. The problems are:
1. After i uploaded a document, i click on the link, it will open automatically, without let user to save that file.
2. Only .txt file can be open and all the characters can be see and read clearly. Other file types such as .doc, .jpg, it only can see those weird characters.
Is that anything wrong with my download codes below?
<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
include 'db_connect.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);
exit;
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="" method="post" name="form1" target="_self">
<?php
include 'db_connect.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="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
</a> <br>
<?php
}
}
?>
</form>
</body>
</html>
Please help me out if anyone knows where is the problem, thanks alot.