Hi,
It's been a while since I posted a question but now I'm really stuck.
The PHP below is what's causing my trouble. I need to be able to track when a member downloads a file, this forms part of the recommendation system.
The code does record the download, however, it doesn't actually download the file. The result is that a file of the correct name appears where it should but the size is 0kb. this is the same regardless of the file type.[php]
require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/dataObject.php' ;
$loDB = new dataObject();
$lnLeaderID = $_GET['id'] ;
$lnFileID = $_GET['item'] ;
$lcFilePath = $_GET['extra4'] ;
// first check that the leader does not already have the download associated with them, if they do there is no reason to load it again.
$lcCheckSQL = "SELECT a.ID, a.frequency + 1 as newFrequency from leaderdownloadassociation a WHERE a.leaderID = $lnLeaderID AND a.downloadID = $lnFileID" ;
$laAssociation = $loDB->queryGetData(false, $lcCheckSQL) ;
if($laAssociation)
{
// update the frequency
$lnFrequency = $laAssociation[0]['newFrequency'] ;
$lcUpdate = "UPDATE leaderdownloadassociation set frequency = $lnFrequency, editdate = now() WHERE leaderID = $lnLeaderID AND downloadID = $lnFileID" ;
$loDB->iQuery($lcUpdate) ;
} else // first download
{
$lcTableName = "leaderdownloadassociation" ;
$lcFieldList = "leaderID, downloadID, frequency, createdate, editdate" ;
$lcValueList = "$lnLeaderID, $lnFileID, 1, now(), now()";
$loDB->queryInsert($lcTableName, $lcFieldList, $lcValueList, false) ;
}
// the calling file
$lcCallingFile = $_SERVER['HTTP_REFERER'] ;
$lcBaseCall = substr($lcCallingFile, 1, strpos($lcCallingfile, '?')-1) ;
// download the file
header("Content-Type: " . $_GET['extra2']);
header("Content-Length: " . $_GET['extra3']);
header('Content-Disposition: attachment; filename="' . $_GET['extra'] .'"');
if(file_exists($lcFilePath))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($lcFilePath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($lcFilePath));
ob_clean();
flush();
readfile($lcFilePath);
}
[/php]
The URL that calls this from the click of link may be something like:
[html]
<a title="John Kirkby" href="lib/recorddownload.php?item=2&id=1&extra=JohnKirkbyCAP pm.mp3
&extra2=audio/mpeg&extra3=5484191
&extra4=http://www.christianleadership.org.uk/download/audio/JohnKirkbyCAPpm.mp3"
class="inlineLink"
id="download2">John Kirkby</a>
[/html]
I've checked the permissions on the file and they are fine - 755, basically the file can be read by anyone, plus a bit more.
Can anyone spot the problem with this or suggest a better way of achieving the same result.
Cheers
nathj