Having Trouble

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
zinek
Forum Newbie
Posts: 2
Joined: Tue Jun 09, 2009 8:43 pm

Having Trouble

Post by zinek »

I've been having a hard time trying to figure out how to code up a download counter. So I have a database setup with a bunch of different fields for certain attributes; one of them is downloads which pertains to the number of downloads and the other is a link to the file. I'm trying to figure out how to code up a counter so when that specific file is downloaded it will add 1 to downloads. The database is setup so there is a different file for each entry so it would have to be setup so it would work for the specific id number.

I did quite a few searches looking for this specific type of code but they all seem very lengthy and deal with users and what not. To me it seems like i can be coded of very concisely but I haven't been able to get a grasp on how to do it.

on specific id page > download file > download number goes up by 1. Pretty much the gist of what I'm looking to do. Any suggestions?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Having Trouble

Post by requinix »

The most reliable way is to make yourself a script dedicated to handling file downloads. Something like

Code: Select all

<?php
 
if (!isset($_GET["id"])) {
    // no download requested
    // redirect to your main page? don't do anything?
    exit;
}
 
$id = $_GET["id"];
// look up file type and location using $id
 
// option a: update database here (increment download counter)
 
header("Content-Type: $file_type");
readfile($file_location);
 
// option b: update database here (increment download counter)
 
?>
Option A: the counter you have will keep track of every time the download is requested. It will count the times when the user stops the download while PHP is still trying to send it.
Option B: the counter tracks the number of times the download is completed... almost: it won't know if the user canceled the download in the time between PHP outputting the file and the browser receiving the file.
zinek
Forum Newbie
Posts: 2
Joined: Tue Jun 09, 2009 8:43 pm

Re: Having Trouble

Post by zinek »

Awesome, thanks, now hopefully i can get this working properly.
Post Reply