0

My php script is supposed to download a .zip file, and increment a counter. In the code below, the parameters passed in on the URL are not being recognized, so the line to download a file doesn't do anything, and if the file download doesn't work, the count loops endlessly, incrementing the count. My provider is using PHP V5.2.

I'd like for the passed parms to work, but I can live with hardcoding "myapp.zip" in the tag.

I need to return to the page that called count.php after it's work is done.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
//$Down=$_GET['Down'];
$Down=$_Post['Down'];
echo "File:" . $Down;?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

  <meta content="en-us" http-equiv="Content-Language" />
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  <!--meta http-equiv="refresh" content="0;url=<?php echo $Down;?>"/-->
  <meta http-equiv="refresh" content="0;url=MyApp.zip"/>
</head>
<body>
  <?php
   $filePath = 'count.txt';
   // If file exists, read current count from it, otherwise, initialize it to 0
   $count = file_exists($filePath) ? file_get_contents($filePath) : 0;
   // Increment the count and overwrite the file, writing the new value<br />
   file_put_contents($filePath, ++$count);
   // Display current download count
   //echo "Downloads:" . $count;
   //header("Location: $r.htm");
?>
</body>
</html>

It's called from r.htm like this:

<form method="post" action="count.php?Down=myapp.zip" style="text-align: center">
<input type="submit" value="Download MyApp">
</form>
6
  • You cant do it with php only. Client made request to HTTP-server and server responce that content-type is ZIP, after downloading that cant send new header with redirect Commented Mar 24, 2013 at 21:13
  • your question is not clear, do you want to write count + 1 to 'count.txt' then redirect to the previous url? Commented Mar 24, 2013 at 21:24
  • @barif - I was afraid of that. I suppose I'll have to put a button on it. I really wanted it to be transparent. Commented Mar 24, 2013 at 21:43
  • @razzak - Download the file, increment the counter, return to r.htm. Commented Mar 24, 2013 at 21:45
  • does it have to be in php only? you can use ajax to increment the counter without having to redirect the user! Commented Mar 24, 2013 at 21:49

2 Answers 2

1

Here you go (tested)

Count.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php

if(isset($_POST['down'])){
$down = 'myapp.zip';
}

echo "File: <a href='$down'>$down</a>";

if(!isset($_POST['down'])){
die('NO ACCESS');

// or give them a link to go to the form and click on the button
//die('<a href="formdown.htm">Use the form to download</a>');
}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

  <meta content="en-us" http-equiv="Content-Language" />
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  <!--meta http-equiv="refresh" content="0;url=<?php echo $Down;?>"/-->

</head>
<body>
  <?php
   $filePath = 'count.txt';
   // If file exists, read current count from it, otherwise, initialize it to 0
   $count = file_exists($filePath) ? file_get_contents($filePath) : 0;
   // Increment the count and overwrite the file, writing the new value<br />
   file_put_contents($filePath, ++$count);
   // Display current download count
   //echo "Downloads:" . $count;
   //header("Location: $r.htm");
?>
</body>
</html>

Form:

<form method="post" action="count.php" style="text-align: center">
<input type="hidden" name="down" value="$file" />
<input type="submit" value="Download MyApp" />
</form>
Sign up to request clarification or add additional context in comments.

21 Comments

Mike, this could also be modified so that if someone tries to view the source to download it directly, value="myapp.zip" could be changed to value="$file"; in count.php with code as $file= "(path_to)myapp.zip"; (also tested).
_ Thanks, but it's just not seeing the parms on the URL. r.htm blinks, the counter is incremented, and r.htm is still in the browser.
@Mike I didn't add the redirection to r.htm. I figured you could modify the script to redirect to your preferred URL. Just comment out the //header("Location: $r.htm"); - However $r.htm and r.htm are two different things.
value="$file" ? you can't use php variables in html code unless you wrap it with <?php ?> and in this case it is undefined even if you wrap it!
@razzak It worked for me, I tested it. Copy the source files and upload one called myapp.zip. ;-)
|
1

This version Mike, increments the counter if the user clicks the download button, which will immediately prompt the user to save the file (somewhere on their computer).

count.php

<?php

if(isset($_POST['down'])){

$filePath = 'count.txt';
   $count = file_exists($filePath) ? file_get_contents($filePath) : 0;
   // Increment the count and overwrite the file, writing the new value<br />
   file_put_contents($filePath, ++$count);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=myapp.zip");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");

readfile("myapp.zip");
}

if(!isset($_POST['down'])){
//die('NO ACCESS');

die('<a href="form.htm">Use the form to download</a>');
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>

</head>

<body>

</body>
</html>

The form:

<!DOCTYPE html>
<head>

</head>

<body>

<form method="post" action="count.php" style="text-align: center">
<input type="hidden" name="down" value="file" />
<input type="submit" value="Download MyApp" />
</form>

</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.