Help with Downloading a file in PHP/MySQL

Hi All,

I’m having an issue with downloading files in php. I have them uploaded to a folder called upload and there path, name, type and size stored in a db in a table called tblUplod. When i download the file through php it doesn’t recognize the type of file. So when i try to download file it gets saves as ‘filename.exe’. Below is my code for when i Uploaded the file:


<?php

//start the session 
session_start(); 

//check to make sure the session variable is registered 
if(isset($_SESSION['id']))
{

include ("connect.php");
$Sid = $_SESSION['id'];

$uploadDir = 'C:/wamp/www/FYP/upload/';



if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}



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

$query = "INSERT INTO tblUpload (`TeacherID`,`Name`, `Size`, `Type`, `Path` ) ".
"VALUES ('$Sid','$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());


echo "<br>Files uploaded<br>";

}
} 
else{ 

//the session variable isn't registered, send them back to the login page 
header( "Location: login1.html" ); 
} 

?> 


And here is the code for downloading the file:


<?php

if(isset($_GET['id']))
{
include 'connect.php';

$id = $_GET['id'];
$query = "SELECT Name, Type, Size, Path FROM tblUpload WHERE UploadID = '$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;
}

If anybody can help me with this I will extremely appreciate it because I’m new to PHP and can’t figure it out!!

Thank you!!

You need to tell the browser what sort of file it is
header(‘Content-type: application/pdf’);