I'm a .NET guy attempting a PHP thing here, so am totally out of my comfort zone right now. What I THINK I want to do is to have 3 files:
download.php
:(a) contains a lookup of IDs to filenames (so
download.php?file=11
querystring tells me I should host abc.zip)(b) Some code to log this download to
stats.log
(c) A couple
header()
calls and areadfile()
call, similar to the answer to this questionstats.log
: A simple log file that might look like the following example. This allows for logging to be accomplished by simply appending a line of text yet allows me to condense it from time to time.
abc.zip 1234
xyz.zip 4321
abc.zip 1
abc.zip 1
abc.zip 1
xyz.zip 1
abc.zip 1
stats.php
: This is ultimately the PHP file that serves the stats. They can be real-time or near real-time, perhaps re-reading the file every minute and caching it or whatever. I don't really care and this won't be hit all that often but I do need to make sure that this isn't a stupidly expensive operation. This need not be a pretty page. Something so a human can easily read it is all that matters, so no fancy requirements there. For the above example ofstats.log
, I'd like this to serve something like the following:
abc.zip: 1238 downloads
xyz.zip: 4322 downloads
Ultimately, I don't want a database or any other systems involved in this. I only have FTP access to the server, so I can't really do much other than place scripts into the directory. I realize that I'll need to make sure that the script has write permissions to stats.txt
, which is fine.
So my questions. I have a number of them but I believe they're all quite easy for somebody who knows PHP.
- I think I have the hosting portion of
download.php
understood by setting headers and using readfile. However, how could I have a collection of key/value pairs representing file ids and filenames? If I were in .NET, I could do something like:var foo = new Dictionary<int, string> {{11, "abc.zip"}, {12, "xyz.zip"}}
but I don't have a clue what this looks like in PHP. - How do I get querystrings? I need to pull from the URL "stuff/download.php?file=11" and take the 11 to grab my "abc.zip" out of my lookup collection.
- How do I write the newline to my
stats.log
file? - How do I loop through my
stats.log
file in mystats.php
script to count up and host these stats? - Bonus question: How do I cache the results from step 4 and only read the file once every minute/hour/or whatever?
I can probably fill in some gaps if somebody can answer at least most of these questions, but help sure would be appreciated! :)