PHP url参数并返回到调用页面

PHP url参数并返回到调用页面,php,Php,我的php脚本应该下载一个.zip文件,并增加一个计数器。 在下面的代码中,URL上传递的参数无法识别,因此下载文件的行没有任何作用,如果文件下载不起作用,计数将无休止地循环,增加计数。我的提供者使用的是PHPV5.2 我希望传递的参数能够工作,但我可以在标签中硬编码“myapp.zip” 工作完成后,我需要返回名为count.php的页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.

我的php脚本应该下载一个.zip文件,并增加一个计数器。 在下面的代码中,URL上传递的参数无法识别,因此下载文件的行没有任何作用,如果文件下载不起作用,计数将无休止地循环,增加计数。我的提供者使用的是PHPV5.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>

给你(已测试)

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

表格:

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

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

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

表格:

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


您不能仅用php来完成。客户端向HTTP服务器发出请求,服务器响应内容类型为ZIP,下载后无法发送带有重定向的新标题。您的问题不清楚,是否要将count+1写入“count.txt”,然后重定向到以前的url?@barif-我很害怕。我想我得在上面按个按钮。我真的希望它是透明的。@razzak-下载文件,增加计数器,返回到r.htm。它必须只使用php吗?您可以使用ajax增加计数器,而无需重定向用户!Mike,这也可以修改,如果有人试图查看源代码直接下载,则可以将
value=“myapp.zip”
更改为
value=“$file”
count.php
中,代码为
$file=“(path_to)myapp.zip”(也经过测试)。\谢谢,但它只是看不到URL上的参数。r、 htm闪烁,计数器递增,r.htm仍在浏览器中。@Mike,我没有将重定向添加到
r.htm
。我想你可以修改脚本重定向到你喜欢的URL。只需注释掉
//标题(“Location:$r.htm”)-但是
$r.htm
r.htm
是两个不同的东西。
value=“$file”
?除非用
包装php变量,否则不能在html代码中使用php变量,在这种情况下,即使包装它,它也是未定义的@拉扎克:它对我有效,我测试过。复制源文件并上传一个名为
myapp.zip
;-)