PHP url params并返回调用页面

时间:2013-03-24 21:09:10

标签: php

我的php脚本应该下载一个.zip文件,并递增一个计数器。 在下面的代码中,无法识别在URL上传入的参数,因此下载文件的行不会执行任何操作,如果文件下载不起作用,则计数会无休止地循环,从而递增计数。我的提供者正在使用PHP V5.2。

我希望传递的parms能够正常工作,但我可以在标签中使用硬编码“myapp.zip”。

我需要在完成工作后返回调用count.php的页面。

<!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>

从r.htm调用它是这样的:

<form method="post" action="count.php?Down=myapp.zip" style="text-align: center">
<input type="submit" value="Download MyApp">
</form>

2 个答案:

答案 0 :(得分:1)

你去(测试)

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

答案 1 :(得分:1)

此版本的Mike,如果用户点击下载按钮,会递增计数器,这将立即提示用户保存文件(在计算机上的某个位置)。

<强> 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>

表格:

<!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>