trying to create a download counter

Discussion in 'PHP' started by swapshop, Dec 22, 2008.

  1. #1
    Trying to use this script

    1 <?php
    2 $download_dir = 'downloads'; // the folder where the files are stored ('.' if this script is in the same folder)
    3 $counter_dir = 'counters'; // the folder where your counter files are stored
    4 /*
    5 Save this script as download.php
    6 each file to download must have a .txt-file called like "filename.ext.txt" in the 'counters' folder -
    7 call the counter e.g. like this: <? include("counters/filename.pdf.txt"); ?>
    8 download the file [download.php?get=name_of_file]
    9 */
    10 $path = $download_dir.'/'.$HTTP_GET_VARS['get'];
    11 if(file_exists($path))
    12 {
    13 $file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','r+');
    14 $count = fread($file,100);
    15 fclose($file); // closes file
    16 $count++;
    17 $file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','w'); // opens file again with 'w'-parameter
    18 fwrite($file, $count);
    19 fclose($file);
    20 $size = filesize($path);
    21 header('Content-Type: application/octet-stream');
    22 header('Content-Disposition: attachment; filename='.$HTTP_GET_VARS['get']);
    23 header('Content-Length: '.$size);
    24 readfile($path);
    25 }else{
    26 echo "<font face=$textfont size=2>";
    27 echo "<center><br><br>The file [<b>$get$extension</b>] is not available for download.<br>";
    28 echo "Please contact the web administrator <a href='http://www.yoursite.com</a>here";
    29 }
    30 ?>

    Calling from within my cms

    <p>
    <?
    error_reporting("E_ALL");
    include("counters/file.rar.txt"); ?>
    </p>
    <p>
    <?
    error_reporting("E_ALL");
    $download =("<a href='download.php?get=visitors-map-thedropzone.rar'>Download Source here</a>");
    echo $download;
    ?>

    Issues is when you click on the link you get download.php only not the actual file?
     
    swapshop, Dec 22, 2008 IP
  2. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
      $fileID=intval($_GET['id']);
      $row=mysql_fetch_assoc(mysql_query("SELECT * FROM `files` WHERE `id`='".$fileID."'"));
      if ($_POST['subdownload']) {
        if ($row['id']>0) {
          mysql_query("UPDATE `files` SET `count`=`count`+'1' WHERE `id`='".$fileID."'");
          header("Location: files/".$row['filename']);
          exit;
        } else {
          //file not found - handle error message here
        }
      }
      if ($_POST['subcancel']) {
        header("Location: filelist.php");
        exit;
      }
    ?>
    <html>
    <head>
      <title>File Information: <?=$row['title']?></title>
    </head>
    <body>
      Show some file details here now a form to allow the user to download it<br />
      <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        <input type="submit" name="subdownload" value="Download File" /> or <input type="submit" name="subcancel" value="Cancel" />
      </form>
    </body>
    </html>
    Code (markup):
    Gives you the basic idea - no formatting or showing file info - that's up to you to do.

    All it does is get the file ID number from the URL then gets the file info from the database.

    The rest of the PHP is skipped and the HTML would show the file information along with a form and two submit buttons.

    Clicking "Cancel" aborts and redirects to filelist.php

    Clicking "Download" increments the download counter by 1 and redirects to the file prompting a download.
     
    Yesideez, Dec 22, 2008 IP
  3. apsam29

    apsam29 Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thankz Yesideez...
    Yours is good idea to get faster couting....
     
    apsam29, Dec 22, 2008 IP
  4. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thats great but could you post the sql information that is needed?
     
    swapshop, Dec 27, 2008 IP