开发者

PHP flat file download counter

开发者 https://www.devze.com 2023-01-14 08:09 出处:网络
I got this freely available php-script to track download statistics. Simple really, it just count downloads and pass it on t开发者_如何学运维o a log file and another script uses that log file to displ

I got this freely available php-script to track download statistics. Simple really, it just count downloads and pass it on t开发者_如何学运维o 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&file=filename.zip>

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消