Downloading From Folder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ciaranholland
Forum Newbie
Posts: 6
Joined: Sat Feb 20, 2010 6:50 pm

Downloading From Folder

Post by ciaranholland »

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:

Code: Select all

 
<?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:

Code: Select all

 
<?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!!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Downloading From Folder

Post by s.dot »

What is $type in your download script? echo $type and see what it is and make sure it's a valid type being sent.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
rmrbest
Forum Newbie
Posts: 8
Joined: Fri Feb 26, 2010 2:32 am

Re: Downloading From Folder

Post by rmrbest »

Try put in upload script echo mime_content_type($fileName) and return for exmaple to test.gif -> image/gif what is content-type.
Post Reply