0

I have an Apache html server running which contains the standard directory listing. Is it possible to add an extra parameter to show the number of downloads beside each file?

3 Answers3

1

Mod_AutoIndex does not have any such functionality. I don't know of any module that does or way of doing this short of an external script.

You could create a quick script that counts the downloads in the logs for each file, then adds that via the AddDescription directive in a .htaccess file. This would know be able to track downloads that appear in the logs (which generally only go back several days or weeks).

Chris S
  • 77,945
  • 11
  • 124
  • 216
1

You can't do the on a directory listing, what you can do is to place a simple PHP file which will list the directory content (google for "php file list directory contents") and edit it that way to show a download counter next to each file.

You can get the download count by parsing the Apache access log file.

But this setup is not recommended for a large site, with generate a lot of traffic. and will not work on a share hosting where you don't have access to the server log files.

I'm not familiar with your setup, so i can't give a more specific answer.

Rabin
  • 187
  • 6
0

I'd go for a jQuery event listener on the download link, and have it all done server side through PHP. It may not be foolproof, but it doesn't impact on the user as much.

If you wanted to do something like this, then you first need to group your buttons in a way that is accessible through jQuery; there are other ways to select multiple elements in jQuery but the easiest is by making them all members of a specific class.

You can then loop through the class using the .each() function. So for initialisation of the page; i.e. getting all the page content working; you need to loop through every button and make a post request to the PHP script; all that needs to be sent to the PHP script is the id of the download. This is done in Part 1 of the code below.

Part 2 is setting up the event listener for when a download occurs. Once again, by just putting an event listener on the whole class we can detect a click on any of them; and then identify which button it came from by accessing it's id. (Note: For simplicity's sake; make the id in the markup the same as the id in the database)

This then makes another post request to the php script; but supplies an additional parameter - "download". In reality, the value that corresponds to download doesn't actually matter; because this case is simple enough that the user is either downloading it or not. So in the PHP segment of the code (later below) we don't even check it's value; we just check that it exists.

$(document).ready(function(){
  /* [[PART 1]]: loop through every
element in the .button class*/
  $(".button").each(function (e) {
    /* get it's ID (which is equal to the download id stored in the db */
    var id = e.target.id;
    /* Post the download id to ranking.php; then echo the returned value to an element with the id of 'count<num>'; where num is the download id. */
    $.post( 'ranking.php', { 'id' :

I suggest jQuery as it provides enough syntactical sugar to make things pretty easy. Additionally, by using jQuery - you're opening up the doorway to lots of possibilities in the future if you choose. Server Side is where the PHP takes over. All this needs to do in reality is to check whether download is set, and echo back the number of downloads. (Obviously incrementing the number with the database if the 'download' parameter is set)

This could be achieved with something like ...

<?php
    mysql_connect(localhost,$user,$password);@mysql_select_db($database) or die
( "Unable to select database");
    if(! isset($_POST['id']) {
         //die error!
    }
    /* if $_POST['download'] isn't set, then just get the current download number and echo it back to the browser */
    if(! isset($_POST['download'] ){

The above example would work (obviously with the database functions corrected) for simple cases, but obviously - this probably isnt valid code as I've literally typed it over my dinner! Naturally, it's just to give you an idea of a possible solution. (If you do decide to do similar, I've demonstrated bad practice - mysql operations should preferably be done with mysqli* functions or PDO now)

Look up the PHP My SQL documentation; hopefully this has pointed you in the right direction. Now alternatively, as I know you aren't too keen on jQuery (it's great once you get in to it!) the PHP script that you'd use with the jQuery solution can be used on it's own. Simply changing $_POST[] to $_GET[] and, as other answers have said, using URLs like ranking.php?id=; however then you need to look at getting the PHP script to pass the file on after the database queries have finished.

It also leaves a question as to the displaying of the download number; as without AJAX you will have to embed some PHP in the HTML mark-up inline. I hope I've edited this to make a bit more sense, and given you an overview of the other option!

Colt
  • 2,029
  • 6
  • 21
  • 27