2013-03-24 114 views
0

我的PHP腳本應該下載一個.zip文件,並增加一個計數器。 在下面的代碼中,傳遞給URL的參數無法識別,因此下載文件的行不起任何作用,並且如果文件下載不起作用,則計數循環無休止地增加計數。我的提供者正在使用PHP V5.2。PHP的url參數並返回到調用頁面

我希望通過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> 
+0

你不能只用PHP做到這一點。客戶端請求HTTP服務器和服務器響應該內容類型是ZIP,下載後不能發送新的標題與重定向 – Barif 2013-03-24 21:13:31

+0

你的問題不清楚,你想寫count + 1'count.txt'然後重定向到以前的網址? – razzak 2013-03-24 21:24:10

+0

@barif - 我害怕這一點。我想我必須在上面放一個按鈕。我真的希望它是透明的。 – Mike 2013-03-24 21:43:23

回答

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> 
+0

邁克,這也可以修改,以便如果有人試圖直接查看源代碼下載它,'value =「myapp.zip」'可以改爲'count =「$ file」;''count.php'代碼爲'$ file =「(path_to)myapp.zip」;'(也測試過)。 – 2013-03-24 21:53:49

+0

_謝謝,但它只是沒有看到URL上的參數。 r.htm閃爍,計數器遞增,並且r.htm仍在瀏覽器中。 – Mike 2013-03-24 21:59:52

+0

@Mike我沒有將重定向添加到'r.htm'。我想你可以修改腳本重定向到你的首選URL。只需註釋掉// header(「Location:$ r.htm」);' - 然而'$ r.htm'和'r.htm'是兩個不同的東西。 – 2013-03-24 22:01:28

1

這個版本的邁克,如果用戶點擊下載按鈕,它會立即提示用戶保存文件(他們的計算機上的某個地方)遞增計數器。

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>