dongmuyan5638 2019-02-15 22:12
浏览 39

如何使用php mysqli创建下载文件计数

I have a download.php file which enables me download my files from database, I need help to alter it to count the number of times a file has been downloaded on my webpage.

download.php
<?php
require_once './includes/connection.php';
$conn = dbConnect('read');
//if(isset($_GET['id'])){}
$songid = $_GET['id'];
$sql = 'SELECT * FROM homemp3 WHERE song_id ='.$songid;
$result = $conn->query($sql);
$row = mysqli_fetch_array($result);
$mp3 = 'homemp3/' .$row['filename'];
if(file_exists($mp3)) {
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename="' .basename($mp3). '"' 
);
header('Content-length: '. filesize($mp3));
header('Cache-Control: no-cache');
header('Content-Transfer-Encoding: binary'); 
readfile($mp3);
exit;
}

download_script.php
<div class="col-12">
<div class="media">
<img src="./images/<?= $mainImage; ?>" alt="PowerBeatz" width="300" 
height="300" />
<div class="media-body">
<h5 class="mt-0"><span class="title"><?php echo $row['song']; ?></span> 
</h5>
<?php echo $row['artist']; ?>
<div class="play">
<a href="download.php?id=<?php echo $row['song_id']; ?>" class="btn btn- 
primary"><i class="fa fa-download"></i></a>
</div>
<div>

<p>Total Downloads:</p>
</div>
  • 写回答

1条回答 默认 最新

  • dongzj2015 2019-02-15 22:37
    关注

    The simple way is you need to create a counter column in your table, (ex : download_count) and every time user download your the file then update the record download_count+=1.

    ...
    if(file_exists($mp3)) {
    
        // Your counter code here
    
        header('Content-Type: audio/mpeg');
        header('Content-Disposition: attachment; filename="' .basename($mp3). '"' 
    );
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备