Reputation: 13
I have a webpage which helps download files from database. I want to count the how many times the download button has been clicked and display it on my webpage with unique id. This is my download webpage. Any help will do please
<div class="container">
<div class="row">
<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="file.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>
</div>
</div>
</div>
</div>
</div>
this is my file.php script and I need a script to show me how to alter it to display the count results on the download page, please any help
<?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'];
// Get a date and timestamp
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);
header('Location: ' . $_GET['url']);
exit;
}
My database structure...
song_id filename song album_art artist downloads
x x x x x x
Upvotes: 1
Views: 1966
Reputation: 396
Each time the file is accessed, could be in file.php, do an UPDATE in MYSQL with the file id's row, update a column called 'downloads' or something. Update it, adding 1 to it.
Like this:
UPDATE songs SET downloads=downloads+1 WHERE id = song_id
The on the above page, you'd get the downloads column and echo it after 'Total Downloads'.
Not knowing whether you are using some framework you could leverage for this, if you are just writing straight PHP from scratch you could use the above like this. Here is an updated downloads.php:
<?php
ob_start();
try {
// Database connection
require_once './includes/connection.php';
$conn = dbConnect('read');
// Sql query from table
if(isset($_GET['id'])){
$songid = $_GET['id'];
}
$sql = 'SELECT filename, album_art, downloads, song, song_id, artist FROM homemp3 WHERE
song_id ='.$songid;
$result = $conn->query($sql);
$row = mysqli_fetch_array($result);
<!doctype html>
<div class="container">
<div class="row">
<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="file.php?id=<?php echo $row['song_id']; ?>" class="btn btn-primary"><i class="fa fa-download"></i> </a>
</div>
<div>
<p>Total Downloads: <?php echo $row['downloads']; ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
And then file.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'];
// Update download count
$conn = dbConnect('read');
$update = 'UPDATE homemp3 SET downloads=downloads+1 WHERE song_id ='.$songid;
$downloads = $conn->query($update);
// Get a date and timestamp
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);
header('Location: ' . $_GET['url']);
exit;
}
Not knowing what connection.php looks like, I think this will work. Give it a try!
Upvotes: 2