Results 1 to 5 of 5

Thread: number of downloads

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    number of downloads

    I have a page with a few different files that can be downloaded. I'd like to keep track of how many times those things are being downloaded.

    I've been thinking this over and I'm guessing the only way to do this would be to make the download link open up a page that inserts a value into a DB field, correct?

    Is there any way to do this without opening a new page?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    here's some rough theory:

    you're going to need one of those "your download will start in 5 seconds, or click here" pages.. or, well, you could make the link be like "download.php?id=4354" and then when you go to that link, don't send anything to the browser, but query the database instead, and send a location header to the file.. it should work.

    something like this:

    PHP Code:
    <?
      /*******************************
       * download.php by david miles *
       *******************************/
      //get the file id
      if(!empty($_GET['id']) && is_numeric($_GET['id'])){
        $fileid = $_GET['id'];
      }else{
        $fileid = 0;
      }

      if(checkID($fileid)){
        //id is valid
        //get the file's name from the database
        $q = mysql_query("SELECT filename FROM table WHERE id='$fileid'");
        $q = mysql_fetch_array($q);
        $file = "http://www.site.com/files/" . $q['filename'];
        if(file_exists($file)){
          //update the download count
          updateDL($fileid);
          //redirect the user
          header("Location: $file");
        }else{
          error(1);
        }
      }else{
        //id isn't valid
        error(0);
      }
      function updateDL($id){
        //update the database
        $q = "UPDATE table SET count=count+1 WHERE id='$id'";
        mysql_query($q);
      }
      function error($id){
        switch($id){
          case 0;
            echo "Invalid file ID!";
            break;
          case 1;
            echo "File does not exist!";
            break;
        }
        exit;
      }
    ?>
    might have some syntax errors and such, built that off of memory and is untested.. but i guess it points you in a general direction, so i hope it helps.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by kows
    something like this:

    PHP Code:
    <?php

      
    if(checkID($fileid)){
          
    //...
      
    }

    ?>
    You didn't define a checkID() function, unless it already exists within PHP and I don't know about it?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    crap, I must've forgotten that.. that was a busy day. I actually have the function, I meant to grab it from something I made before and add it in, but anyway, it's something like this:

    PHP Code:
    function checkID($id){
      
    $q mysql_query("SELECT id FROM table WHERE id='$id';");
      
    $q mysql_fetch_array($q);
      if(
    $q[0] > 0){
        return 
    $q[0];
      }

    anyways, sorry about that
    Like Archer? Check out some Sterling Archer quotes.

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by kows
    crap, I must've forgotten that.. that was a busy day. I actually have the function, I meant to grab it from something I made before and add it in, but anyway, it's something like this:

    PHP Code:
    function checkID($id){
      
    $q mysql_query("SELECT id FROM table WHERE id='$id';");
      
    $q mysql_fetch_array($q);
      if(
    $q[0] > 0){
        return 
    $q[0];
      }

    anyways, sorry about that
    If you ask me, kows, that's overkill right there. I don't have time to type up my alternative, but you basically have the same query here:

    Code:
    $q = mysql_query("SELECT filename FROM table WHERE id='$fileid'");
    In the if statement. If the query returns no results, you know the ID was invalid.

    I just don't think you should run two database queries when you can get away with one.

    Just my 7 cents.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width