I have a download script that worked just fine under a directory in my website but when I use the same script in a level higher it wants to download the php file containing the script instead of the actual file. The file's name is docs_download.php and so it tries to download this file instead of the file in my database. Here is the code:
Code: Select all
<?php
/*
* use this script to enable your visitors to download
* your files.
*/
require('config.php');
require('admin/global.php');
if (isset($_GET['file']) || isset($_GET['filename']) ){
$dir = 'files';
if ( isset($_GET['file']) ){
//Download by file ID
$fid = $_GET['file'];
$results = mysql_query("SELECT filename,filesize,dcount,dmonth FROM files WHERE id=$fid");
if (mysql_numrows($results) == 0){
echo "<b>File not found</b>";
return;
}
$file = stripslashes(mysql_result($results,0,"filename"));
// Combine the download path and the filename to create the full path to the file.
$file = "$dir/$file";
// Test to ensure that the file exists.
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist. It may have been removed.");
}else{
//Download by file name
$file = $_GET['filename'];
$results = mysql_query("SELECT id,filename,filesize,dcount,dmonth FROM files WHERE filename='$file' LIMIT 1");
if (mysql_numrows($results) == 0){
echo "<b>File not found</b>";
return;
}
$fid = mysql_result($results,0,"id");
}
addToDownloadCount($fid);
}
else{
echo "<b>File not found</b>";
return;
}
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize("$dir/$file"));
header('Content-Disposition: attachment; filename=' . $file);
readfile("$dir/$file");
exit;
?>Thanks,