0

我的 php 脚本应该下载一个 .zip 文件,并增加一个计数器。在下面的代码中,在 URL 上传入的参数没有被识别,所以下载文件的行没有做任何事情,如果文件下载不起作用,计数将无限循环,增加计数。我的提供商使用的是 PHP V5.2。

我希望传递的参数能够工作,但我可以在标签中硬编码“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>
4

2 回答 2

1

给你(测试)

计数.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>
于 2013-03-24T21:45:30.607 回答
1

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

计数.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>
于 2013-03-25T11:04:41.230 回答