【问题标题】:How to COUNT the number of times a FILE from my database has been download?如何计算从我的数据库下载文件的次数?
【发布时间】:2015-12-07 00:16:52
【问题描述】:

我在尝试让我的系统计算文件下载次数时遇到问题。该网站具有链接到存储在我的数据库中的文件的下载按钮,我希望能够计算每个文件的下载量以显示为统计数据,理想情况下,一旦他们单击下载链接,文件表应该增加,但我不能这样做。我是 php 的初学者。有人可以帮助我吗?这是我显示文件的代码。

              <h3 class="box-title">Senarai Borang Cuti</h3>
              <p>&nbsp;</p>
            </div><!-- /.box-header -->
            <div class="box-body">  
            <!-- Advanced Tables -->
                <div class="table-responsive">
                <?php
                    $raw_results = mysql_query("SELECT * FROM document WHERE doc_jenisfail= 'Cuti';"); 

                                        ?>

                    <table width="98%" class="table table-striped" id="dataTables-example">
                        <thead>
                                                        <table width="600" border="1">
      <tr>

      <th width="190" bgcolor="#756E37"class="sw" style="text-align: centre" scope="col">Tajuk Borang</th>
      <th width="30" bgcolor="#756E37" class="sw" style="text-align: centre" scope="col">Tarikh Upload</th>
      <th width="30" bgcolor="#756E37" class="sw" style="text-align: centre" scope="col">Memuat Turun</th>

    </tr>
                        </thead>
                        <tbody>
                        <?php 
                            while($results = mysql_fetch_array($raw_results)) {     ?>
                            <tr>
                                <td align="left"><?php echo $results['nama_file'];?></td>         

                                <td align="center"><?php echo $results['tanggal_upload'];?></td>

                                <td align="center"><a href="admin/<?php echo $results['file'] ?>" target= "_blank"><img src="images/download.png?key=<?php $results['nama_file'] ?>" /></a></td>

                            </tr>
                            <?php  } ?>
                            </tbody>
                        </table>
                          </p></td>

【问题讨论】:

  • 如果admin/&lt;?php echo $results['file'] ?&gt;是指向实际文件的链接,那么您可以在点击链接时使用javascript onClick()进行绑定。然后使用 ajax 将值发送到 php 以更新数据库计数器。
  • 当有人访问其中一个 url 的 admin/filename 等时,您不会在数据库中增加一个字段吗?换句话说,您不会在给定文档中做的事情,但是文件在哪里实际下载,因为预期内容处置和不同的标头用于下载等。
  • 我不知道该怎么做.. :( @Sean
  • 我是 php 初学者,我真的不熟悉 .. :( @adeneo

标签: php html download count increment


【解决方案1】:

您可以为数据库中的下载添加 +1,然后重定向到文件。

在您的模板中使用download.php?id=1 之类的链接,用于下载 id 为 1 的文件。

下载.php代码:

<?php 
//name of file: download.php

$id = $_GET['id'];

//Check if the get number is really a number
if (ctype_digit($id)) {

//Add +1 to downloads
mysql_query("update `document` set `downloads`=`downloads`+1 where id='$id'") or die(mysql_error());

//Redirect to downloads
$download_file = mysql_fetch_array(mysql_query("select * from document where id='$id'")) or mysql_error();

header("Location: http://example.com/pdfs/$download_file[name].pdf");

} else {
  echo 'Wrong link';
}


?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2020-06-29
    • 2021-11-19
    • 1970-01-01
    相关资源
    最近更新 更多