The SQL
CREATE TABLE counter ( id int(5) DEFAULT '0' NOT NULL auto_increment, FileName varchar(50) NOT NULL, FileURL varchar(255) NOT NULL, Count int(5) NOT NULL, PRIMARY KEY (id) );
Basically the sql table has fields for the id, which is unique for each file, the file name, the file url, and the count.
The viewing of the file.
View.php
<?php include "connect.php"; $x ="SELECT * from counter order by FileName"; $result = mysql_query($x) or die ("Could not execute query : $x." . mysql_error()); echo "<p align=center><b>File Download</b></p>"; echo "<ol>"; while ($row=mysql_fetch_array($result)) { $id = $row["id"]; $FileName = $row["FileName"]; $FileURL = $row["FileURL"]; $Count =$row["Count"]; echo "<li><a href=download.php?id=$id>$FileName</a></li>"; } ?>
First you connect then you select the downloads and order them by the filename, and if sql will not connect the script dies immediately. Then you echo File Download fetch the resaults of your previous query and order them out to look pretty, then link them!
Now for the download counter, and of course the download!
Download.php
<?php // Database connection stuff here $x ="SELECT * from counter where id='$id'"; $result = mysql_query($x) or die ("Could not execute query : $x." . mysql_error()); while ($row = mysql_fetch_array($result)) { $id = $row["id"]; $FileName = $row["FileName"]; $FileURL = $row["FileURL"]; $Count = $row["Count"]; header('Location: '.$FileURL); $x ="update counter set Count=Count+1 where id='$id'"; $result = mysql_query($x) or die ("Could not execute query : $x." . mysql_error()); } ?>
Ok I shall explain this part as well. First you query the database and get the id of the file selected. Then it finds it or the script automatically dies. Define the id filename fileurl and the times downloaded. It redirects the user to the file, and adds 1 more number to the download stats.