Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:48:36+00:00 2026-05-24T10:48:36+00:00

I’m a .NET guy attempting a PHP thing here, so am totally out of

  • 0

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 a readfile() call, similar to the answer to this question

  • stats.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 of stats.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.

  1. 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.
  2. 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.
  3. How do I write the newline to my stats.log file?
  4. How do I loop through my stats.log file in my stats.php script to count up and host these stats?
  5. 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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:48:37+00:00Added an answer on May 24, 2026 at 10:48 am

    1- You are looking for array e.g.

     $files=array(11=>'abc.zip',
                 12=>'xyz.zip');
    

    2- The Query String is accessed by the super global $_GET, so in your case $_GET['file'] holds that data you are interested in.

    3,4,5
    I would recommend storing the information JSON encoded. e.g.

    $rawInfo=file_get_contents('stats.log');
    $Info=json_decode($rawInfo,true);
    if(isset($Info[$_GET['file']])){
        $Info[$_GET['file']]++;
    }else{
        $Info[$_GET['file']]=1;
    }
    $rawInfo=json_encode($Info);
    $h=fopen('stats.log','c');// If $h is false, you couldn't open the file
    flock($h,LOCK_EX);// lock the file
    $b=fwrite($h,$rawInfo);// if $b is not greater than 0, nothing was written
    flock($h,LOCK_UN);
    fclose($h);
    //And then actually serve the file requested
    

    This has the advantage of storing the information already in a useful format.

    Whenever you fetch out the json_decodeed data, it is in the format of an array, which you will need to know how to handle.

    stats.php might look something like this:

    $rStats=file_get_contents('stats.log');
    $Stats=json_decode($rStats,true);
    foreach($Stats as $k=>$v){
       echo $k.': '.$v.' download'.($v==1?'':'s');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.