1

I'm looking for a way to send a user a regular file (mp3s or pictures), and keeping count of how many times this file was accessed without going through an HTML/PHP page.

For example, the user will point his browser to bla.com/file.mp3 and start downloading it, while a server-side script will do something like saving data to a database.

Any idea where should I get started?

Thanks!

Burkhard
  • 14,596
  • 22
  • 87
  • 108
adizhol
  • 11
  • 3
  • 3
    You could parse log files. Otherwise no, you should be intercepting requests with a file which serves the content after updating a counter in a database. – user229044 Sep 21 '12 at 13:18
  • 1
    I agree with meagar. If it is performance you're worried about, then you should: 1) Intercept the request with your PHP script that does the logging, 2) Set [X-Sendfile](http://stackoverflow.com/questions/80186/using-x-sendfile-with-apache-php). – RickN Sep 21 '12 at 13:26
  • Another approach would be to have a webserver module log the requests into a database. No idea if something like this already exists though. – Gerald Schneider Sep 21 '12 at 13:33

3 Answers3

1

You will need to go through a php script, what you could do is rewrite the extensions you want to track, preferably at the folder level, to a php script which then does the calculations you need and serves the file to the user.

For Example:

If you want to track the /downloads/ folder you would create a rewrite on your webserver to rewrite all or just specific extensions to a php file we'll call proxy.php for this example.

An example uri would be proxy.php?file=file.mp3 the proxy.php script sanitizes the file parameter, checks if the user has permission to download if applicable, checks if the file exists, serves the file to the client and perform any operations needed on the backend like database updates etc..

Jason Brumwell
  • 3,482
  • 24
  • 16
0

Do you mean that you don't want your users to be presented with a specific page and interrupt their flow? If you do, you can still use a PHP page using the following steps. (I'm not up to date with PHP so it'll be pseudo-code, but you'll get the idea)

  • Provide links to your file as (for example) http://example.com/trackedDownloader.php?id=someUniqueIdentifer
  • In the tracedDownloader.php file, determine the real location on the server that relates to the unique id (e.g. 12345 could map to /uploadedFiles/AnExample.mp3)
  • Set an appropriate content type header in your output.
  • Log the request to your database.
  • Return the contents of the file directly as page output.
ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
  • ZombieSheep and @Jason Brumwell: I'm not 100% sure, but to send content directly as output, I'll need to use the header() function, right? I thought that I couldn't do anything in the code before sending the headers (and also the file)... Thanks – adizhol Sep 21 '12 at 16:14
0

You would need to scan log files. Regardless you most likely would want to store counters in a database?

There is a great solution in serving static files using PHP:

https://tn123.org/mod_xsendfile/

Miroslav
  • 1,960
  • 1
  • 13
  • 26