Hello out there!

I use the following download counter to count the files that I have avaliable for download on my website:
download.php:

$extension = ""; //leave this blank if you have various extensions.
$counterdir = "counters/"; //change this if you want other directory for the counter files.

$textfont = "Verdana,Arial"; //text font for the error msg
if (file_exists("$get$extension"))
{
header("location: $get$extension"); // download the file [download.php?get=name_of_file]
$file = fopen("$counterdir/$get.txt","r"); // download counter
$count = fread($file, 100);
$countplus = ($count + 1);
fclose($file);
$fileb = fopen("$counterdir/$get.txt","w");
fwrite($fileb, $countplus, 100);
fclose($fileb);
}
else echo "<font face=$textfont size=2>";
echo "<center>

The file [$get$extension] is not available for download.
";
echo "Please contact the web administrator";

?>

I call this script more or less like this in a href: download.php?get=myfile.pdf

Now, I would like to add this following script that forces the users' browser to download the files (usually .pdf files) instead of opening them in the browser window:
forcedownload.php:

<?
$path = "myfolders/".$HTTP_GET_VARS['filename'];
$file = basename($path);
$size = filesize($path);

header ("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: $size");

readfile($path);
?>
I call this script in a href like this: forcedownload.php?filename=myfile.pdf

Both scripts work fine individually but how do I combine them so that they both work at the same time? So that by clicking a link, the file is forced to be saved and counted at the same time?

Thanks in advance. :-)

    Just put the second script before the header() call in the first script. You'll probably need to change some variable names too.

      Hi Shrike.

      I'm a totally newbie to php, sorry...could you be so kind to show me what you mean? And which variables need changing and to what?

        <?php
        error_reporting("E_ALL");
        
        $extension = ""; //leave this blank if you have various extensions.
        $textfont = "Verdana,Arial"; //text font for the error msg
        
        $file = $_SERVER['DOCUMENT_ROOT']."/".$_GET['file'];
        $counterdir = $_SERVER['DOCUMENT_ROOT']."/counters/"; //change this if you want other directory for the counter files.
        if (file_exists($file)){
        	$count = file_get_contents($counterdir."/".$_GET['file'].".txt");
        	$fileb = fopen($counterdir."/".$_GET['file'].".txt","w");
        	fwrite($fileb, ++$count);
        	@fclose($fileb);
        	header("Content-Type: application/octet-stream");
        	header("Content-Disposition: attachment; filename=".basename($file));
        	header("Content-Length: ".filesize($file)."");
        	$data = file_get_contents($file);
        	print $data;
        }else{
        	echo "<font face=$textfont size=2>";
        	echo "<center>The file [".$_GET['file']."] is not available for download.";
        	echo "Please contact the web administrator";
        }
        // Usage: script.php?file=somefile.pdf
        ?>
        

        It's kinda sad when fixing other peoples code beats working 🙂

          Hi Shrike.

          Thanks a lot for your help so far. I saved your script as script.php but I get this error: "The file [] is not available for download. Please contact the web administrator"

          It seems like the path is wrong - or...?

          Before, when I used the 'force browser download-script alone, I had my downloadable files in a folder named 'myfolders/' - how and where do I specify this location in your script?

            $file = $_SERVER['DOCUMENT_ROOT']."/myfolders/".$_GET['file']; 
            

              I'm sorry to bother you again. :-) The webpage that contains the links to my downloadable files is located in the folder 'myfolders' along with the downloadable files so what do I need to change in order to point to the same folder as the script itself is located in? (Btw, in 'myfolders' I have the folder 'counters' which contains the txt-files for the download counter.)

                Hello all.

                Here's a solution that works for me. It forces the browser to download a file instead of opening it and at the same time it counts the number of downloads.

                <?php 
                
                $download_dir = 'downloads'; // the folder where the files are stored ('.' if this script is in the same folder) 
                $counter_dir = 'counters'; // the folder where your counter files are stored 
                
                /* Save this script as download.php 
                // each file to download must have a .txt-file called like "filename.ext.txt" in the 'counters' folder - 
                // call the counter like this: <? include("counters/filename.pdf.txt"); ?> 
                // download the file [download.php?get=name_of_file]*/ 
                
                $path = $download_dir.'/'.$HTTP_GET_VARS['get']; 
                
                if(file_exists($path)){ 
                $file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','r+'); 
                $count = fread($file,100); 
                fclose($file); // closes file 
                $count++; 
                $file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','w'); // opens file again with 'w'-parameter 
                fwrite($file, $count); 
                fclose($file); 
                
                $size = filesize($path); 
                
                header('Content-Type: application/octet-stream'); 
                header('Content-Disposition: attachment; filename='.$HTTP_GET_VARS['get']); 
                header('Content-Length: '.$size); 
                
                readfile($path);   
                
                }else{ 
                
                echo "<font face=$textfont size=2>"; 
                echo "<center><br><br>The file [<b>$get$extension</b>] is not available for download.<br>"; 
                echo "Please contact the web administrator <a href='http://www.yoursite.com</a>here"; 
                } 
                
                ?> 

                Enjoy!

                  Write a Reply...