...

View Full Version : How to count the downloads number of a file



Mido™
11-03-2009, 06:26 PM
I need to know how to count the downloads number or hits number for a file who uploaded in my site.
hope if any one can help.

met
11-03-2009, 06:47 PM
you can use either a text file if it's just one file, or extend this with a database + table, allowing for more advanced features etc.

both ways are simple to execute.

Mido™
11-03-2009, 07:33 PM
you can use either a text file if it's just one file, or extend this with a database + table, allowing for more advanced features etc.

both ways are simple to execute.

no the files will be many,and about the solution of using mysql can you give me just the key or the general view of it?

met
11-03-2009, 09:00 PM
i would create a table like this



CREATE TABLE `your_database`.`tblDownloads` (
`file_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY
`file_name` VARCHAR( 50 ) NOT NULL ,
`file_location` VARCHAR( 50 ) NOT NULL ,
`upload_date` DATETIME NOT NULL ,
`download_hits` INT( 5 ) NOT NULL DEFAULT '0' ,
PRIMARY KEY ( `file_id` )
) ENGINE = MYISAM


every time you add a new file to your library of files do an insert query with the necessary information.

Set up a downloads page LIKE



$get_downloads = mysql_query("SELECT * FROM tblDownloads");
while($row=mysql_fetch_array($get_downloads)) {
echo '<a href="download.php?file_id='.$row['file_id'].'">Download '. $row['file_name'] . '</a><br />';
}



on download.php have something LIKE



if(isset($_GET['file_id'])) {
$file_id = $_GET['file_id']; /* validate this */


$update=mysql_query("UPDATE `your_database`.`tblDownloads` SET `download_hits` = `download_hits` +1 WHERE `tblDownloads`.`file_id` =". $file_id . " LIMIT 1");

if($update) { 'thanks for downloading...'; }

Mido™
11-03-2009, 11:46 PM
Thanks much or you help,but I have a problem.
the files didn't downloaded!
when I want to download a file on my server I set the attribute "href" of anchor = the file name.
my download link is look like this:


<a href='files/$row[file_name]?do=hit&num=$row[id]' > $row[file_name] </a>

and I added the part of "?do=hit&num=$row[id]" after I saw you last post but I the variable of GET didn't set.
are you have a solution or another way to download the file who in your server?

met
11-04-2009, 12:03 AM
need more code to see if you did something wrong but the line you provided suggests you haven't done it correctly

should be something like



echo '<a href="files/'.$row['file_name'].'?do=hit&num='.$row['id'].'">'. $row['file_name'] . '</a>';


also look in to http://php.net/manual/en/function.header.php



<?php
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
?>


for downloads via php, might be of relevance

Mido™
11-04-2009, 03:22 PM
need more code to see if you did something wrong but the line you provided suggests you haven't done it correctly

should be something like



echo '<a href="files/'.$row['file_name'].'?do=hit&num='.$row['id'].'">'. $row['file_name'] . '</a>';





this didn't worked also,the get array didn't exist.




also look in to http://php.net/manual/en/function.header.php



<?php
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
?>


for downloads via php, might be of relevance

I see this way is more suitable and thanks for provide it,I tried it and it work well but not by 100%.
I found 2 problems:
- the count increase by 3 after downloading the file although I set LIMIT = 1 in the update query.
- the downloaded files size is 0, with any type.

Mido™
11-05-2009, 07:42 PM
hope if any one can help me in this serious problem,I tried many ways and it didn't work.

Mido™
11-07-2009, 06:56 PM
problem solved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum