Page 1 of 1

Website (download) question

Posted: Thu Oct 24, 2002 2:21 pm
by jelleghys
How can you count how much a file is downloaded from your site?

Re:Website (download) question

Posted: Thu Oct 24, 2002 4:38 pm
by smartguy240
Store it as an integer var. make it itself = itself + 1


SMG240

Re:Website (download) question

Posted: Fri Oct 25, 2002 6:50 am
by jelleghys
Yeah, I could have thought of that myself... :) But how? Is it possible to do it with Java?

Re:Website (download) question

Posted: Fri Oct 25, 2002 7:48 am
by AGI1122
Well I do it with php, you have to have a way to save the variable, either to a file, or to a database... not sure if javascript can do that?

Re:Website (download) question

Posted: Fri Oct 25, 2002 8:43 am
by jelleghys
I don't Java can (I was hoping, but...). And I can't run PHP on my website I think (server from school :'( )

Re:Website (download) question

Posted: Fri Oct 25, 2002 9:00 am
by AGI1122
Well I can't do anything for you, the only thing I know how to do it with is php.

Anyway, I wrote a script to count downloads, if anyone else might want it here it is:

Code: Select all

<?php
error_reporting(E_ALL ^ E_NOTICE);
if (file_exists($_GET['file']) && $_GET['file'] != '') {
  $file = file('download.txt');
  for ($i = 0; $i < sizeof($file); $i++) {
    $file["$i"] = str_replace("\r\n",'',$file["$i"]);
    $current_line = explode('|^|',$file["$i"]);
    if ($current_line['0'] == $_GET['file']) {
      $download_line = $i;
    }
  }
  if ($download_line == '0' || $download_line != '') {
    $line = explode('|^|',$file["$download_line"]);
    $line['1'] = $line['1'] + 1;
    $file["$download_line"] = $line['0'].'|^|'.$line['1'];
    $file = implode("\r\n",$file);
    $file2 = fopen('download.txt','w');
    fwrite($file2,$file);
    fclose($file2);
  }
  else {
    $j = sizeof($file) + 1;
    $file["$j"] = $_GET['file'].'|^|1';
    $file = implode("\r\n",$file);
    $file2 = fopen('download.txt','w');
    fwrite($file2,$file);
    fclose($file2);
  }
  header('Location: '.$_GET['file']);
}
else {
  die('File does not exist.');
}
?>
Just paste this code inside of a php file(for instance, download.php or index.php). Then make a blank file called download.txt

Upload both of these files to your server in the same directory as your downloadable files.

Then chmod download.txt 777 so it has write access.

Next change all your links to use the script for now on for downloading files.

This is how it would work for exampe:

http://www.yoursite.com/download.php?file=file_name.zip

Then when they go to that url it will download the file called file_name.zip and either add that file to download.txt or make the counter go up by 1.(if that file is already in downloads.txt).

Hope this script can help someone... I wrote it a few minutes ago,(before I read that you can't use php on your server. ::))

Re:Website (download) question

Posted: Fri Oct 25, 2002 9:16 am
by jelleghys
I tried it anyway, but it didn't work (I really can't use PHP), it just shows the code...

Thanks anyway.
:'( :'( :'(

Re:Website (download) question

Posted: Fri Oct 25, 2002 9:19 am
by AGI1122
Well sorry it doesn't work for you. But still it works just fine so anybody with a server that has php installed can use it.