2015-05-09 53 views
1

我正在使用一个简单的downloadcounter来计算txt文件中的下载量。 在文件夹下载我有一个名为的download.php文件中是这样的代码:下载后如何重定向到linkpage

<?php 
$Down=$_GET['Down']; 
?> 

<html> 
<head> 
<meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>"> 
</head> 
<body> 

<?php 

$filePath = $Down.".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 
file_put_contents($filePath, ++$count); 

// Display current download count 
echo "Downloads:" . $count; 

?> 

</body> 
</html> 

我使用这个链接是在下载文件夹之外的文件,看起来像这样:

<a href="downloads/download.php?Down=download.zip">Download</a> 

到目前为止,这工作得很好。 点击链接后,网址给我:http://localhost/downloads/download.php?Down=download.zip并下载zip文件。

我想实现的是:立即重定向回链接页面;我在的download.php

// Display current download count 
//echo "Downloads:" . $count; 
header('Location: http://localhost/link.php'); 

添加页眉位置回声后现在他立即追溯到和他也有1递增的文本文件,但zip文件不再被下载。 为什么重定向后文件不再被下载?

+0

在header()开始之前不会回应任何内容,似乎你已经禁用了error_reporting? –

+0

我已经禁用回声,因为它不再有用。但问题是,重定向后文件不再被下载... –

+0

你的下载头在哪里? http://php.net/manual/en/function.header.php#example-4905 –

回答

2

请尝试以下操作以下载该文件。

header('Content-Disposition: attachment; filename="'.$Down.'"'); 
+0

它的工作原理!不应引用thnx解决方案 –

+2

'filename'。 – Prateek

3

主要的做法是,你先去“之后”页面然后开始下载。在你的情况下,你想重定向用户到link.php,所以你需要首先将页面重定向到link.php,这应该有你的真实下载url的HTML刷新meta-equiv标签,它会下载文件,并不会影响当前页面。

由于您的前后页面相同,您可以传递link.php?download=true的参数。如果下载是true,则打印meta-equiv标签。

我希望你能得到基本的想法。

1

我使用了另一种方法,用户点击下载链接页面上的链接,而不离开当前页面,计数器实时递增。因此,不需要重定向到另一个或同一个页面,因为该文件是透明计数的,而不会离开当前页面。下载计数器imho应该是透明的,所以它更安全,没有人应该直接访问php脚本。另外请注意,我的首选方法是使用文件扩展名来限制操作,因此用户只能下载某些文件,同时请注意,更好的方法是使用.htaccess拦截匹配的文件并将它们传递给php脚本...最后,我还集成到我的项目的jQuery因此计数器在通过AJAX实时更新​​......所以没有必要重新加载页面看到的结果...

的.htaccess(位于根/文件/)

RewriteEngine on 
RewriteRule ^(.*).(rar|zip)$ /php/doing_download.php?file=$1.$2 [R,L] 

doing_download.php(位于根/ PHP)

<?php 
    $downloads_folder = $_SERVER['DOCUMENT_ROOT'] . '/files/'; 
    $counters_folder = $_SERVER['DOCUMENT_ROOT'] . '/files/counter/'; 
    if (!empty($_GET['file'])) { 
    $file = basename($_GET['file']); 
    $type = array("zip", "rar"); 
    $exts = strtolower(substr(strrchr($file, "."), 1)); 
    if (!in_array($exts, $type)) { 
     header("HTTP/1.0 403 Forbidden"); 
     exit('File not allowed!'); 
    } else { 
     if (file_exists($downloads_folder . $file)) { 
     if (file_exists($counters_folder . md5($file) . '_counter.txt')) { 
      $fp = fopen($counters_folder . md5($file) . '_counter.txt', "r"); 
      $count = fread($fp, 1024); 
      fclose($fp); 
      $fp = fopen($counters_folder . md5($file) . '_counter.txt', "w"); 
      fwrite($fp, $count + 1); 
      fclose($fp); 
     } else { 
      $fp = fopen($counters_folder . md5($file) . '_counter.txt', "w+"); 
      fwrite($fp, 1); 
      fclose($fp); 
     } 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename="' . $file . '"'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Connection: Keep-Alive'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     header('Content-Length: ' . sprintf("%u", filesize($downloads_folder . $file))); 
     $fh = fopen($downloads_folder . $file, "rb"); 
     while (!feof($fh)) { 
      echo fgets($fh); 
      flush(); 
     } 
     fclose($fh); 
     exit; 
     } else { 
     header("HTTP/1.0 404 Not Found"); 
     exit('File not found!'); 
     } 
    } 
    } 
?> 

count_download.php(位于根/ PHP /)

<?php 
    function get_download_count($file = null) { 
    $counters = $_SERVER['DOCUMENT_ROOT'] . '/files/counter/'; 
    if ($file == null) return 0; 
    $count = 0; 
    if (file_exists($counters . md5($file) . '_counter.txt')) { 
     $fp = fopen($counters . md5($file) . '_counter.txt', "r"); 
     $count = fread($fp, 1024); 
     fclose($fp); 
    } else { 
     $fp = fopen($counters . md5($file) . '_counter.txt', "w+"); 
     fwrite($fp, $count); 
     fclose($fp); 
    } 
    return $count; 
    } 
?> 

call_download。PHP(位于根/ PHP)

<?php 
    include($_SERVER["DOCUMENT_ROOT"] . "/php/count_download.php"); 
    $item['item1'] = get_download_count('exampleA.zip'); 
    $item['item2'] = get_download_count('exampleB.zip'); 
    echo json_encode($item); 
?> 

static_download.php(位于根/ PHP)

<?php 
    include($_SERVER["DOCUMENT_ROOT"] . "/php/count_download.php"); 
    $item['item1'] = get_download_count('exampleA.zip'); 
    $item['item2'] = get_download_count('exampleB.zip'); 
?> 

download.js(位于根/ JSC /)

$(document).ready(function() { 
    $.ajaxSetup({cache: false}); 
    getStatus(); 
}); 
function getStatus() { 
    $.getJSON('php/call_download.php', function(data) { 
    $('#item1').html(data.item1); 
    $('#item2').html(data.item2); 
    }); 
    setTimeout("getStatus()",1000); 
} 

index.php(位于root /)

<?php include($_SERVER["DOCUMENT_ROOT"] . "/php/static_download.php"); ?> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Download Page</title> 
<script type="text/javascript" src="jsc/jquery.min.js"></script> 
<script type="text/javascript" src="jsc/download.js"></script> 
</head> 
<body> 
File: <a href="files/exampleA.zip">exampleA.zip</a>&nbsp; - &nbsp;Downloaded <span id="item1"><?php echo $item['item1']; ?></span> Times<br /> 
File: <a href="files/exampleB.zip">exampleB.zip</a>&nbsp; - &nbsp;Downloaded <span id="item2"><?php echo $item['item2']; ?></span> Times<br /> 
File: <a href="test/test.zip">test.zip</a><!-- this file never will be counted since is located in other folder --><br /> 
</body> 
</html> 

根据劳伦斯Cherone和我的答案Here, you can download all files and test them!

https://stackoverflow.com/a/29105023/4432311

PS:你可能需要改变PHP脚本文件夹...我的脚本假设你在你的服务器的根目录测试...

+0

非常感谢! –

+0

我很高兴听到你发现有用我的回答:) – Alessandro