Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-11-2003, 10:58 PM   PM User | #1
Dylan Leblanc
Regular Coder

 
Join Date: Sep 2002
Location: British Columbia
Posts: 235
Thanks: 0
Thanked 0 Times in 0 Posts
Dylan Leblanc is an unknown quantity at this point
My monotored file download script

I wanted to keep stats on some files I have available for download, so I wrote this simple
script to do so. It keeps track of who downloads what files, and how many times.

Users are identified by $memberID

The FileDownload table stores the access information, and contains these columns:

filename - the name of the file downloaded, PK

memberID - the user who downloaded the file, PK

count - number of times the file is downloaded by the user, default 0

PHP Code:
<?

// setup enviroment

require_once('global.php');


// valid file types and their MIME content-types

$contenttypes = array(
    
'png' => 'image/png',
    
'pdf' => 'application/pdf'
);


// the file for download is passed as the query string

$filename basename($_SERVER['QUERY_STRING']);

$file "/usr/home/www/download/{$filename}";

// if file does not exist, or not valid content-type, then 404

if (($filename == '') || (!in_array($fileextensionarray_keys($contenttypes))) || (!file_exists($file))) {
    
header("HTTP/1.0 404 Not Found");
    exit;
}


// file specified is valid

// see if user has previously downloaded this file

$row mysql_fetch_object(mysql_query("SELECT count FROM FileDownload WHERE filename = '{$filename}' AND memberID = {$memberID}"));

// if they have then increase count, if not then count = 1

if ($row) {
    
$count $row->count 1;
} else {
    
$count 1;
}

// do a REPLCE to store new count value

mysql_query("REPLACE FileDownload (filename, memberID, count) VALUES ('{$filename}', {$memberID}, {$count})");


$fileextension substr($filename, -3);

$contenttype $contenttypes[$fileextension];

$filesize filesize($file);

// send HTTP headers to present user with download and save dialog

header("Content-type: {$contenttype}");
header("Content-Disposition: attachment; filename=\"{$filename}\"");
header("Content-Length: {$filesize}");

// now spit out the file

$filepointer fopen($file'r');

print 
fread($filepointer$filesize);

fclose($filepointer);

exit;

?>

The one thing that I wish could be cleaned up is the SELECT and REPLACE queries, and accompanying code to increase $count,
but I see no other way to get the current count from the database otherwise. At first I tried:
REPLACE FileDownload (filename, memberID, count) VALUES ('{$filename}', {$memberID}, count + 1)

but the "count + 1" part does not work because REPLACE actually deletes the row and then inserts it, so the count value is lost.
Dylan Leblanc is offline   Reply With Quote
Old 03-12-2003, 10:05 AM   PM User | #2
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
I think you can do that within an UPDATE call with MySQL

$update_row = mysql_query("UPDATE `table` SET fieldname=fieldname+1 WHERE .........");

am sure that worked for me a while back - might even have been SET fieldname++

any chance you could confirm my suspicions? ie tell us if that works?
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.
Ökii is offline   Reply With Quote
Old 03-12-2003, 10:12 AM   PM User | #3
Dylan Leblanc
Regular Coder

 
Join Date: Sep 2002
Location: British Columbia
Posts: 235
Thanks: 0
Thanked 0 Times in 0 Posts
Dylan Leblanc is an unknown quantity at this point
I use REPLACE so that if the necessary row does not already exist it will be created. If the user has never downloaded a file, then there would be no row in the database to update.

Although good suggestion. I could do UPDATE first, and then if it returns 0 rows affected I could then INSERT, acheiving the same result I already have but with the chance of fewer queries (if row already exists).
Dylan Leblanc is offline   Reply With Quote
Old 02-03-2004, 10:50 AM   PM User | #4
sitami
New Coder

 
Join Date: Jan 2004
Location: UK
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
sitami is an unknown quantity at this point
so you know Okii its fieldname=fieldname+1
sitami is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:43 AM.


Advertisement
Log in to turn off these ads.