【问题标题】:How to count the number of user downloads如何统计用户下载次数
【发布时间】:2012-01-16 09:20:38
【问题描述】:

此代码用于提取文件目录和标题。用户单击文件链接时下载。我需要计算下载次数。用 cakephp 或 php 怎么做?

downloads_controller.php:

function index() {
    $this->set('downloads', $this->paginate());
}

下载/index.ctp:

<?php
    $title = $download['Download']['title']; 
    // output filetitle 
    $filename = '/files/'.$download['Download']['filename']; 
    // output http://localhost/tet/files/un5titled0.rar
    echo $this->Html->link($title, $filename,array('escape' => false)); 
?>

【问题讨论】:

    标签: php cakephp cakephp-1.3


    【解决方案1】:

    恐怕不是这样。

    您要么需要从一个动作重定向(在重定向之前计算),要么使用媒体视图来传递它。 我就是这样做的。 在操作中,您可以在发送文件之前提高计数。

    【讨论】:

    • 在此处查看解决方案:stackoverflow.com/questions/3185049/count-number-of-hits - 另请参阅 cakephp 文档中的 MedieView 了解如何将文件作为下载发送
    • 我认为是在 cakephp 文档中使用 MedieView 的最佳方式,但我仍然不知道如何使用它。文档谈到使用媒体视图从特殊文件夹中提取文件不算数
    • 嗯,当然,你必须实现第一个链接。在将变量传递给视图之前,在此操作中编写您自己的 updateAll() 查询。
    • 我觉得这本书很不言自明:book.cakephp.org/2.0/en/views/…
    • thanks mark..但它没有包含任何使用媒体视图来计算下载量的示例
    【解决方案2】:

    如果您想计算下载量,您应该创建一个为这些下载量提供服务的函数,并在您的数据库中创建一个字段,每次调用此函数时都会增加下载量。例如

    通过$filename$id 从您的视图调用以下函数

    在第一次使用时试用,将 ID=4 作为您数据库中的下载 ID 之一

    http://www.yourdomain.com/downloads/download/4
    

    然后你的控制器将是......

    Class DownloadsController extends AppController{
        // All your other functions here
    
        function download($id = null){
    
            $this->Article->updateAll(
                array('Download.count' => 'Download.count+1'),
                array('Download.id' => $id)
            );
    
            $download = $this->Download->findById($id);
    
            $this->set('filename', $download['Download']['filename']);
    
            //$filename is an array that can then be used in your view to pass the file for download. to find out what is has you can use debug($filename) in your view
            }
    }
    

    然后您需要创建一个特殊的布局,以便浏览器知道请求文件是下载文件,并且您还需要为 download.ctp 创建一个视图。基本上,当您单击页面上的文件链接时,它将调用此函数,该函数将使用其视图来提供文件。

    您可以访问以下内容,了解需要做什么。

    TUTORIAL LINK

    【讨论】:

    • 感谢 caboone,但它不起作用..当下载次数没有增加..当有人下载​​时,我无法理解如何在此函数中处理 $filename 或 $id
    • 我更改了我的功能并提供了更多见解以及教程链接
    • Caboone ..这是很棒的文章..但这不是我需要的..不需要将文件保存在数据库中..我将其保存到目录中..我明白这一点但我想念了解视图和控制器之间的关系,使某人从这样的链接下载 yourdomain.com/downloads/download/4 而不是这样的 yourdomain.com/downloads/file.rar
    • 基本上使用yourdomain.com/downloads/download/4,您告诉控制器将文件名传递给该文件名的ID为4的视图。然后控制器将根据这些条件查看数据库并返回到查看相应的文件。此时我不确定如何处理下载,但您很可能需要创建一个 download.ctp 布局,其中的 告诉浏览器正在提供的文件是下载文件。
    • 感谢卡博恩,我自己得到了答案..你明白我的意思..我把它写在这里给每个需要这个代码的人
    【解决方案3】:

    有很多技术,但最简单的方法是使用文本文件来完成。

    创建一个 txt 文件并将 0(零)写入其中。

    在您的索引函数中,读取文件的内容。

    $counter = file_read_contents('counter.txt');
    

    将读取值增加 1。

    $counter++;
    

    再次将新值写入文件。

    file_put_contents('counter.txt', $counter);
    

    因此,它会计算下载次数并将数量保存在该文件中。

    【讨论】:

    • 我有一个名为 downloads 的表。它有字段(id、文件名、标题),我需要将数据存储在名为 download_count 的新字段中
    • 那么,使用像update table_name set download_count=download_count+1 where id=selected_files_id这样的更新sql
    【解决方案4】:
    function download($id = null) {
        $download = $this->Download->findById($id);
        $this->set(compact('download'));
        if(!empty($id)) {
            // increment the number of items downloads 
            $this->Download->save(array(
                'Download' => array(
                    'id' => $id,
                    'counts' => ($download['Download']['counts'] + 1)
                )
            ));
        }
    
        header('Location: http://localhost/tet/files/'.$download['Download']['filename']);
        exit();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      相关资源
      最近更新 更多