views:

32

answers:

1

Hi everyone, I got this freely available php-script to track download statistics. Simple really, it just count downloads and pass it on to a log file and another script uses that log file to display a top 10 list. So it's exactly what I need so I would really like to make this script work. But it doesn't. Of course. I have limited knowledge in php (but have learned a lot since encountering this script!). According to the author, it has been tested on Apache- and php-versions similar to mine, without problems. So I have been hesitant to actually change any code. I have tried to chmod logfile and/or whole subdirectory to 666 and 777 with no success. My own poor detective work leads nowhere and I have really tried. So I resort to the one thing I usually never do, to ask for help.

The link should have the form:

<a href=http://www.yoursite.com/downloader/download.php?number=1&amp;file=filename.zip&gt;

download.php follows below:

$number = $_GET['number'];

$file = $_GET['file'];


$logfile = "ROOT DIRECTORY PATH/logfile.txt";

$array[dir_1] = "URL PATH/DOWNLOADFILESFOLDER";

    $download = "dir"."_"."$number";

    if ($array[$download]) {


    $fp = fopen($logfile,'r');

     while (!feof($fp))

    {

    $data[] =  chop(fgets($fp,4096));

    }

    fclose($fp);


    $fp = fopen($logfile,"w");

    if (flock($fp,LOCK_EX)) {

    // list and split each filename and number of downloads

    foreach ($data as $lines){
    list($downloads,$filename,$realname) = split("\|",$lines);


        // check if file already exists and add one more download if it does

    if ($filename == $file){

       $downloads++;

        }

        if ($filename <> ""){
        fputs($fp,"$downloads|$filename|$realname\r\n");

        }

    }

    flock($fp,LOCK_UN);

    }

    fclose($fp);




    header("Location: $array[$download]/$file");



}

However one part of the script doesn't make sense to me; where does it obtain the filename and the realname so it can be put into the logfile if the file never has been downloaded before? The list() function and the array of the directory boggles my mind a bit. Though it seems logical. If someone catches some fundamental error or have some tips on how I can continue, it would be highly appreciated.

PS. I am trying to track pdf-files if that helps.

UPDATE! By kind suggestion I ran error reporting and got an error. The faulty scripts had an error in it that I removed. I ran error checking again and only got "Notices" about undefined variables. I changed the error reporting to

error_reporting(E_ALL ^ E_NOTICE);

After that, no errors at all.

+2  A: 

You have to change these:

$logfile = "ROOT DIRECTORY PATH/logfile.txt";
$array[dir_1] = "URL PATH/DOWNLOADFILESFOLDER";

To your actual paths.

Next time, turn on error reporting so you can know when stuff throw errors. One way to do this is adding the following line to the top of your script:

error_reporting(-1) // will show all errors

See: http://php.net/manual/en/function.error-reporting.php

NullUserException
Oh, I did! I just put an explanation there instead for simplicity in the thread.
Xantippa
@Xant Turn on error reporting (see my answer) and tell us what it says.
NullUserException
I got the following error message:Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in /afs/su.se/services/www/hosts/www.ne.su.se/pages/ed/downloader/download.php on line 70That was a bit of a surprise!
Xantippa
@Xant And what is on line 70 (and around it)?
NullUserException
It seems to be the increment counter if the file already exists: line 70--> if ($filename == $file){ $downloads++; }
Xantippa
If the file doesn't exist it will continue to line 73-75 --> if ($filename <> "") { fputs($fp,"$downloads|$filename|\n"); }
Xantippa
@Xant What comes before line 70?
NullUserException
I made an update. But honestly, maybe this is a bit much...
Xantippa