Page 1 of 1

Combining 2 php scripts...

Posted: Tue Jul 06, 2004 8:02 am
by meek
Bech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Hello out there!

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

Code: Select all

$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><br><br>The file ї<b>$get$extension</b>] is not available for download.<br>";
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:

Code: Select all

<? 
$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. :-)

Bech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Jul 06, 2004 10:01 am
by feyd
just put them together (basically), they use seperate code paths... Your counter won't be able to echo anything though..