Hi All,
I'm a newbie.

I need to know if we can write a script that tracks the number of times an xls file was downloaded in a day.

I tried searching many books & websites, but of no use.

Kindly show me the way..

Please..

Thanks

Dani AI

Generated

Several approaches are possible; which one fits depends entirely on how the XLS is accessed. was right to point at access logs for HTTP downloads, suggested filesystem watches, reminded that the file might live on a shared disk, and correctly asked for server/details. The options below map to those cases and give practical, implementable solutions.

For HTTP-served downloads the most reliable method is to force downloads through a small server-side endpoint that increments a counter (DB or flat file) and then streams the file. This avoids log-rotation, proxy/cache, and partial-download pitfalls. Example (minimal Flask + SQLite pattern):

from flask import Flask, send_from_directory, abort
import sqlite3, datetime

app = Flask(__name__)
DB = 'downloads.db'
FILES_DIR = '/var/www/files'

def init_db():
    conn = sqlite3.connect(DB)
    conn.execute('CREATE TABLE IF NOT EXISTS hits(filename TEXT, date TEXT, count INTEGER, PRIMARY KEY(filename,date))')
    conn.commit()
    conn.close()

@app.before_first_request
def setup(): init_db()

def inc(filename):
    today = datetime.date.today().isoformat()
    conn = sqlite3.connect(DB)
    cur = conn.cursor()
    cur.execute('UPDATE hits SET count=count+1 WHERE filename=? AND date=?', (filename, today))
    if cur.rowcount == 0:
        cur.execute('INSERT INTO hits(filename,date,count) VALUES(?,?,1)', (filename, today))
    conn.commit(); conn.close()

@app.route('/download/<path:filename>')
def download(filename):
    if '..' in filename or filename.startswith('/'): abort(400)
    inc(filename)
    return send_from_directory(FILES_DIR, filename, as_attachment=True)

If the XLS is on a shared filesystem (not HTTP), server-side watches are needed. on Linux a persistent audit rule records reads reliably (better than ephemeral inotify for long-term auditing):

sudo auditctl -w /srv/share/filename.xls -p r -k xls_read
ausearch -k xls_read -ts today

(inotify-tools works for short-term monitoring as noted but is not persistent across reboots; on Windows enable Object Access auditing on the file server and filter Security logs).

A JavaScript-only solution is not sufficient when there is no server: client-side handlers can miss right-click/save and will be bypassed by direct access or network shares. If a webpage is used, JS can call a server endpoint (AJAX or navigator.sendBeacon) as a supplement, but server-side counting (or log parsing) remains the authoritative method. Finally, decide whether to count raw GETs, successful streamed transfers, or unique users/IPs per day, and account for caches, CDNs, log rotation, and timezone handling when aggregating "per day" metrics.

Recommended Answers

All 13 Replies

This ugly bit of PHP code works for what we want to do, which sounds similar to what you want. It should be a good starting point.

searchfile is the concatenation of the website's access logs
$key is the filename you want to check for being accessed.

$COUNT=shell_exec("cat searchfile | grep $key | wc");
shell_exec("cat searchfile | grep $key > searchfile2");
shell_exec("rm searchfile");
shell_exec("touch searchfile");

Hi Donald, Thanks a lot for your help.
But where can I find this file "website's access logs"?

Thanks

The webserver should keep track of it. I believe it should be in one of the Apache directories for Apache servers. I'm not too familiar with other servers to know how they work, but I suspect that they are similar in operation.

Do a search on the files on your server for something like, this is the format that they should be in for each time anything on your server is accessed.

xxx.xxx.xxx.xxx - - [06/Jul/2009:16:16:29 -0400] "GET /DIRECTORY/FILE.html HTTP/1.1" 301 360 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"

where xxx.xxx.xxx.xxx is the IP address from which the file is accessed and DIRECTORY/FILE.html is the file we're interested in people accessing.

for more info on the webserver access logs.

Hi donald,
Thanks a lot for the reply.

Am not clear with the code mentioned by you.

How do I search this & where. Please advise..

Do you have FTP access to the actual web server? In the Apache HTTP server directory is where the logs are kept. See the link I posted for details on the log.

The "code" I posted is just the format of the log entry, not really code, per se. If you have access to the web server you would just search the appropriate directory for a file containing that type of syntax.

I'm not using Apache server.

Am unable to search for the access log file.

Please advise

I'm not using Apache server.
Am unable to search for the access log file.
Please advise

I'm not sure of how you could go about doing it then. That's just the solution I came up with to figure out if customers had downloaded update files from our website. It's somewhat convoluted though, perhaps someone else here knows of an easy way to do it.

Good luck!

Can anybody help me with this?
if we can write a script that tracks the number of times an xls file was downloaded in a day.

PLease advise ASAP.

What webserver are you running? What language is the site written in? What access do you have to the server?

You haven't provided enough information for anyone to form an approach....

None of those. This is simply accessing a file on a shared disk. Not a download at all. Take a look at his other threads on this forum about access times and downloads.

Hi All,
My requirements have been changed.
The new reqirmnt is in javascript.

Thanks for all ur responses.

How is javascript going to help with this when there is no server and no html, or anything else that has anything at all to do with JavaScript? Or is this really a download after all? Even though you said it wasn't? Is there a server now? Where did it come from if there wasn't one before? JavaScript would still have nothing to do with it though. Either parse the access log or only allow the "download" through a cgi/jsp/php/whatever rather than a direct link and have the page that delivers the file "increment the counter", as I already suggested in one of your other threads before you claimed that it wasn't a download at all.

if the system is gnu/linux or unix you could use the inotify tools to monitor the file system events

I've made a test and it works quite good for what you're asking for. With this command you could get complete info about accesses to the file in the last tX seconds, check the manpage for more options.

serpiko@PALM-VAIO:/tmp$ inotifywatch -t30 /home/user/filename.xls
Establishing watches...
Finished establishing watches, now collecting statistics.
total access close_nowrite open filename
8 2 3 3 /home/user/filename.xls
Seen in
http://ubuntuforums.org/archive/index.php/t-1121012.html
hope it helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.