Host file, log download, and host stats via PHP?
<p>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:</p>

<ul>
<li><p><strong><code>download.php</code></strong>:</p>

<p>(a) contains a lookup of IDs to filenames (so <code>download.php?file=11</code> querystring tells me I should host abc.zip)</p>

<p>(b) Some code to log this download to <code>stats.log</code></p>

<p>(c) A couple <code>header()</code> calls and a <code>readfile()</code> call, similar to the answer to <a href="http://stackoverflow.com/questions/494016/best-way-to-transparently-log-downloads">this question</a></p></li>
<li><p><strong><code>stats.log</code></strong>: 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.</p></li>
</ul>

<hr>

<pre><code>abc.zip 1234
xyz.zip 4321
abc.zip 1
abc.zip 1
abc.zip 1
xyz.zip 1
abc.zip 1
</code></pre>

<hr>

<ul>
<li><strong><code>stats.php</code></strong>: 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 of <code>stats.log</code>, I'd like this to serve something like the following:</li>
</ul>

<hr>

<pre><code>abc.zip: 1238 downloads
xyz.zip: 4322 downloads
</code></pre>

<hr>

<p>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 <code>stats.txt</code>, which is fine.</p>

<p>So my questions. I have a number of them but I believe they're all quite easy for somebody who knows PHP.</p>

<ol>
<li>I think I have the hosting portion of <code>download.php</code> 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: <code>var foo = new Dictionary&lt;int, string&gt; {{11, "abc.zip"}, {12, "xyz.zip"}}</code> but I don't have a clue what this looks like in PHP.</li>
<li>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.</li>
<li>How do I write the newline to my <code>stats.log</code> file?</li>
<li>How do I loop through my <code>stats.log</code> file in my <code>stats.php</code> script to count up and host these stats?</li>
<li>Bonus question: How do I cache the results from step 4 and only read the file once every minute/hour/or whatever?</li>
</ol>

<p>I can probably fill in some gaps if somebody can answer at least most of these questions, but help sure would be appreciated! :)</p>
