How do I add download count to my file manager?

Hello everyone!
May I ask how to add download count to my site?
Here are my 2 PHP and CSS files, and the image displayed on my page.
I want it to show up next to the size.
Thanks very much!

PHP FILE:

<table class="file-list">
<?php $i = 1;?>

	<tbody>
	
	<?php if(!empty($params['dirs'])) foreach ($params['dirs'] as $file):?>
		<tr class="directory <?php if ($file['type'] == "back") echo 'back-button';?>">
		
		  <?php if (gator::checkPermissions('rw')):?>
			<?php if ($file['type'] != 'back'):?>
			<td class="chkboxes"><input type="checkbox" name="<?php echo $i++;?>" value="<?php echo $file['crypt']?>" /></td>
			<?php else:?>
			<td class="chkboxes"><a class="back" href="?cd=<?php echo $file['link']?>"></a></td>
			<?php endif;?>
		  <?php else:?>
		  	<?php if ($file['type'] != 'back'):?>
			<td class="chkboxes"></td>
			<?php else:?>
			<td class="chkboxes"><a class="back" href="?cd=<?php echo $file['link']?>"></a></td>
			<?php endif;?>
		  <?php endif;?>
		 
			<td class="filename" colspan=3>
			 <?php if ($file['name'] == ".."):?>
				<a href="?cd=<?php echo $file['link']?>"><?php echo lang::get("Go Back")?></a>
		     <?php else:?>
			 	<a <?php if($file['buffer']!=false) echo 'class="'.$file['buffer'].'"';?> href="?cd=<?php echo gator::encodeurl(($file['link']))?>"><?php echo $file['name']?></a>
			 <?php endif;?>
			</td>
			
			<td class="actions">
			 <?php if (gator::checkPermissions('w') && $file['type'] != 'back'):?>
			 <button type="button" class="action-info" data-type="<?php echo $file['type']?>" data-name="<?php echo $file['name']?>" data-crypt="<?php echo $file['crypt']?>" data-time="<?php echo date(gatorconf::get('time_format'), $file['time'])?>"></button>
			 <?php endif;?>
			 
			</td>

	
		</tr>
	<?php endforeach;?>
	
	<?php if(!empty($params['files'])) foreach ($params['files'] as $file):?>
	
		<tr class="file">
	
		  	<?php if (gator::checkPermissions('rw')):?>
			<td class="chkboxes"><input type="checkbox" name="<?php echo $i++;?>" value="<?php echo $file['crypt']?>" /></td>
			<?php else:?>
			<td class="chkboxes"></td>
		  	<?php endif;?>
			
			<?php if (gatorconf::get('allow_file_links')):?>
			 <td class="filename">
			  <a <?php if($file['buffer']!=false) echo 'class="'.$file['buffer'].'"';?> <?php if(gatorconf::get('use_lightbox_gallery') && $file['type'] == 'image') echo 'rel="lightbox[images]"';?> href="<?php echo gator::encodeurl($file['link'])?>" target="_blank"><?php echo $file['name']?></a>
			 </td>
			<?php else:?>
			 <td class="filename">
			  <a <?php if($file['buffer']!=false) echo 'class="'.$file['buffer'].'"';?>><?php echo $file['name']?></a>
			 </td>
			<?php endif;?>
			
			<td class="filesize"><?php echo $file['size']?></td>
			<td class="filetime"><?php echo date(gatorconf::get('time_format'), $file['time'])?></td>

			<?php if (gatorconf::get('allow_file_links') || gator::checkPermissions('w')):?>
			<td class="actions">
			 <button type="button" class="action-info" data-type="<?php echo $file['type']?>" data-link="<?php echo gator::encodeurl($file['link'])?>" data-name="<?php echo $file['name']?>" data-crypt="<?php echo $file['crypt']?>" data-size="<?php echo $file['size']?>" data-time="<?php echo date(gatorconf::get('time_format'), $file['time'])?>"></button>
			</td>
			<?php endif;?>
			
						
		</tr>
	<?php endforeach;?>
	
	<?php if (empty($params['files']) && empty($params['dirs'])):?>
		<tr class="file">
			<td class="actions"> </td>
			
			<td class="filename" colspan=2>
			 <?php echo lang::get("This folder is empty")?>
			</td>
	
		</tr>
	<?php endif;?>


	</tbody>
</table>

CSS FILE

body {
	margin: 30px 0 0 0;
	background-color: #e2e2cd;
	color: #515348;
}

.right{
	float: right;
}

.clear {
	clear: both;
}

.button,
.button *,
button,
button *{
	white-space: nowrap;
}

#logo {
	margin: 0 0 30px 0;
	background-color: #fff;
}

#logo img{
	border: 0;
}

.login #logo img {
	display: block;
	margin: 0 auto;
}

.login input[type="Submit"]{
	width: 120px;
	margin: 20px 0 20px 0;
}

a:hover.button,
table a:hover{
	text-decoration: none;
}


.top-menu {
	position: fixed;
	top: 0px;
	left: 0px;
	z-index: 1000;
	width: 100%;
	height: 30px;
	line-height: 26px;
	background-color: #383838;
	padding: 2px 0 2px 0;
	text-align: right;
	color: #E0FFFA;
	font-size: 12px;
	overflow: hidden;
}

.top-menu a{
	color: #E0FFFA;
	cursor: pointer;
}

.top-menu a:hover{
	text-decoration: underline;
}

.top-menu .row{
	padding: 0 15px 0 15px;
	min-width: 800px;
}

.top-menu-spacer{
	height: 30px;
}

.nav{
    overflow: hidden;
    margin: 0 0 20px 0;
}

.navigation-button-right {
	float: right;
}

.navigation-button-right a,
.navigation-button-right input{
	float: left;
}

#newfolder_button{
	width: 350px;
	margin: -56px 0 0 0;
}

#newfolder_button ul li{
	min-height: 30px;
}


button.upload-done{
	width: 120px;
}


.navigation-button-right input{
	width: 56%;
	height: 37px;
	margin: 0;
	border-radius: 3px 0 0 3px;
}

.navigation-button-right div.radius{
	border-radius: 0 3px 3px 0;
	width: 44%;
}

form#fileset {
	margin: 12px 0 0 0;	
}

#fileset td{
	white-space:nowrap;	
}

table {
    width: 100%;
	white-space: nowrap;
}

table tbody {
    min-height: 50px;
    overflow: hidden;
}

table tbody tr td {
    border: medium none;
    color: #373737;
    padding: 4px 5px;
    line-height: 40px;
    vertical-align: middle;
}

table .chkboxes {
	min-width: 40px;
	width: 40px;
	text-align: center;
}

a.back{
	background: url("img/icons30px.png") no-repeat scroll -30px -360px transparent;
	width: 30px;
	height: 30px;
}

table .actions{
		width: 10%;
}
table .actions button {
	cursor: pointer;
	width: 30px;
	height: 30px;
	border: 0;
	padding: 0;
	margin: 0;
	vertical-align:middle;
}

table .actions button:hover {
	opacity:0.4;
}

table .actions button.action-info
{
	background: url("img/icons30px.png") no-repeat scroll -120px -150px transparent;
	display: block;
}

table .actions button.action-delete-account,
table .actions button.action-account{
	float: right;
}

table .actions button.action-account
{
	background: url("img/icons30px.png") no-repeat scroll -120px -150px transparent;
	display: block;
}

table .actions button.action-delete-account
{
    background: url("img/icons30px.png") no-repeat scroll -150px -240px transparent;
	display: block;
}

table tr.directory a{
	color: #373737;
	font-weight: bold;
}

table td.filename a{
	color: #373737;
}

tr.file td.filename a{
	max-width: 560px;
}

table td.filename a.buffer-cut {
	color: #999999;
}

table td.filename a.buffer-copy {
	color: #000080;
}

.file-list a{
	display: block;
	color: #373737;
}

table .filename{
	text-align: left;
	width: 100%;
	overflow: hidden;
}

table .filesize{
	text-align: right;
	min-width: 70px;
}

table .username{
	width: 150px;
	max-width: 150px;
	min-width: 150px;
	overflow: hidden;
}

table .homedir{
	max-width: 230px;
	overflow: hidden;
}

table .permissions{
	width: 50px;
	min-width: 50px;
	text-align: center;
	font-weight: bold;
}

table .accounts{
	width: 70px;
}

.bottom-actions{
	margin: 0 0 10px 0;
	height: 37px;
}

.bottom-actions button{
	width: auto;
	display: inline;
	padding: 10px 20px 10px 20px;
}

button.select-button{
	width: 130px;
	margin: 0 10px 0 0;
}
div#content {
	padding: 20px 30px 20px 30px;
	color: #515348;
	font-size: 76%;
	line-height: 1.6em;
	height: 1%;
	background: #FFF;
	width: 100%;
}

.reveal-modal{
	padding-top: 40px;	
}

textarea#file-content{
	max-height: 400px;
	resize: none;
}

#big_modal{
	top: 40px;
}

.modal-descr{
	margin: 0 0 20px 0;
}

.modal-content{
	margin: 20px 0 30px 0;	
}

.modal-buttons button{
	margin: 0 0 0 5px;
}

table[role="presentation"] {
	width: 100%;
	display: block;
	min-height: 40px;
}

.rename_content{
	margin: 0 0 20px 0;	
}

.close-reveal-modal{
   background: url("img/icons30px.png") no-repeat scroll -150px -240px transparent;
   width: 30px;
   height: 30px;
}

.close-reveal-modal:hover{
   opacity:0.4;
}


div#top-panel {
	height: 0px;
	margin: 20px 0 0 0;
	overflow: hidden;
}

.small-auth-menu{
	float: right;
	font-weight: bold;
}

#close-top-panel{
	display: none;
	overflow: hidden;
	margin: 0 0 10px 0;
}

tbody.files .name {
	width: 40%;
	max-width: 200px;
	overflow: hidden;
	padding: 0 0 0 10px;
}

tbody.files .progress-col,
tbody.files .error {
	width: 100%;
}

div.progress {
	width: 100%;
}

.tbody.files {
    width: 100%;
}

.template-upload span.ok {
    background: url("img/icons30px.png") no-repeat scroll -180px -240px transparent;
    border: 0 none;
    float: right;
    height: 30px;
    overflow: visible;
    width: 30px;
}

.template-upload button {
    background: url("img/icons30px.png") no-repeat scroll -150px -240px transparent;
    border: 0 none;
    cursor: pointer;
    float: right;
    height: 30px;
    width: 30px;
    margin: 2px 0 0 0;
}

.template-upload button:hover {
	opacity:0.4;
}

div.error {
	margin: 1em 0;
	background: #a62b25;
	border-top: 2px solid #db6e3c;
	border-bottom: 2px solid #db6e3c;
	padding: 5px 10px;
	color: #f2dec4;
	color: #fff;
	font-weight: bold;
}

.login div#wrapper {
	width: 420px;
}

.login div#content{
	overflow: hidden;
}

.main div#wrapper {
	min-width: 800px; 
}

.login div#topcorners {
	width: 420px;
}

.login div#bottomcorners {
	width: 420px;
}

.login div#header h1 a {
	width: 360px; /* 420 - 30 - 30 */
}

div#wrapper {
	text-align: left;
	margin: 0 auto;
}

div#header {
	margin-bottom: 20px;
	padding: 30px 0 0 0;
	color: #515348;
}

div#header h1 {
	margin: 0 30px;
}

div#content {
	padding: 15px 30px 10px 30px;
}

.login div#content input.inputtext {
	color: #515348;
	height: 50px;
	padding: 10px;
	font-size: 130%;
}

.login input[type="Submit"] {
	float: right;
}

#topcorners {
	background: #FFF;
	height: 10px;
	margin: 0 auto;
	position: relative;
	-webkit-border-radius: 8px 8px 0px 0px;
	-moz-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	behavior: url(/PIE.htc);
}

#bottomcorners {
	background: #FFF;
	height: 10px;
	margin: 0 auto;
	position: relative;
	-webkit-border-radius: 0px 0px 8px 8px;
	-moz-border-radius: 0px 0px 8px 8px;
	border-radius: 0px 0px 8px 8px;
	behavior: url(/PIE.htc);
}

.directory-tree{
	background: url("img/icons30px.png") no-repeat scroll -90px -120px transparent;
	cursor: pointer;
	width: 30px;
	height: 30px;
	display: block;
	float: right;
	margin: 0 8px 0 6px;
}

.directory-tree:hover{
	opacity:0.4;
}

.view-style{
	background: url("img/icons30px.png") no-repeat scroll -120px -300px transparent;
	cursor: pointer;
	width: 30px;
	height: 30px;
	display: block;
	float: right;
	margin: 0 0px 0 6px;
}

.view-style:hover{
	opacity:0.4;
}

.filter-field {
	float: right; 
	width: 140px; 
	height: 30px
}

.odd {
	background: none repeat scroll 0 0 #FFFFFF !important;
}

.even {
	background: none repeat scroll 0 0 #F9F9F9 !important;
}

.image-preview {
	width: 80px;
	/*height: 50px;*/	
	margin-right: 20px;
	border: 0;
	position: relative;
	bottom: -8px;
}

.image-size {
	cursor: pointer;
	width: 30px;
	height: 30px;
	display: block;
	float: right;
	margin: 0 0px 0 6px;
	-webkit-touch-callout: none;
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

.image-size.increase{
	background: url("img/icons30px.png") no-repeat scroll -120px 0px transparent;
}

.image-size.decrease{
	background: url("img/icons30px.png") no-repeat scroll -90px 0px transparent;
}

.image-size:hover{
	opacity:0.4;
}


.filter-field .postfix {
	font-size: 85%;
	background: url("img/icons30px.png") no-repeat scroll -60px -240px transparent;
}


#dir-links{
	height: 300px;
	overflow: scroll;
}

#text-search{
	width: 200px;
	float: left;
	height: 37px;
}

.text-search-box{
	float: right;
}

.text-search-box .search-img{
	background: url("img/icons30px.png") no-repeat scroll -60px -240px transparent;
	width: 30px;
	height: 30px;
	margin: 3px 10px 0 0;
	display: block;
	float: left;
}

#dir-links li[clink]{
	cursor: pointer;
	font-weight: bold;
}

#dir-links li[isfile]{
	cursor: default;
	font-weight: normal;
}

#dir-links li{	
	list-style-type: disc;
}

#dir-links{ padding: 5px; background-color: White; margin: 0; } 
#dir-links ul{ padding: 0; margin: 0 0 0 10px; } 
#dir-links li a{ font-family: Arial; text-decoration: none; color: black; } 
#dir-links li a:hover{ color: blue; } 
#dir-links li{ list-style: none; background-image: url("img/dotted.gif"); background-repeat: repeat-y; padding: 2px 0 2px 20px; text-indent: -19px; background-position: 9px 0; } 
#dir-links li:before{ content: url("img/folder.gif") "  "; }
#dir-links li[isfile]:before{ content: url("img/page.gif") "  "; }
#dir-links li{ list-style: none; background-image: url("img/dotted.gif"); background-repeat: repeat-y; padding: 2px 0 2px 20px; text-indent: -19px; background-position: 9px 0; } 
#dir-links li:last-child{ background-repeat: no-repeat; }

div.breadcrumbs {
	margin: 0 8px 0 8px;
	font-size: 14px;
	font-weight: bold;
	height: 30px;
	line-height: 30px;
	overflow: hidden;
	float: left;
	color: #666666;
}

div.breadcrumbs a{
	color: #666666;
}

.version-info{
	text-align: center;
	margin: 0;
	float: left;
	cursor: pointer;
}


#footer {
	margin: 50px 0 0 0;
	height: 30px;
}



Do you already have the download count stored somewhere against the filename, or do you need to implement that as well? I would think the easiest way is to modify whatever code actually performs the download to increment the download count in your database.

1 Like

Currently I don’t have any source code to display downloads, so I would like someone to help me edit this source code so that it shows downloads.
Thanks for the help!

I get that, but what about the questions above? Does your code already store a count of how many times each file was downloaded, so all we need to do is figure out how to display it, or do you also need to implement code to count each download?

1 Like

This link maybe usefu:

https://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php/

Currently my database does not have a load counter table as shown below.
Can anyone guide me how to query database with PHP to get download count.
Thanks very much!

You can’t query what isn’t there. You’re going to need to create a column for number of downloads, and you’ll have to increment that column every time the file is downloaded.

1 Like

The important thing is to modify the source code so that it queries the database every time someone clicks on the link, and displays the download count outside the site.
Please help me, because I am new to PHP & MySQL. I come from Vietnam.

It is, but you can’t do that until you’ve modified your download code to actually store that information. If it’s not in the database (or anywhere else), you can’t display it. And, can you expand on what you mean by “displays the download count outside the site”? If it’s being downloaded outside the site, I can’t see how you will be able to keep count. Or are you referring to the position where you want to display the count?

Do you have specific download code to control who can and cannot download a file, or do you just have a direct link to each file?

1 Like

This is my source code, you can download it and edit it to show downloads.
User: admin
Password: admin123

Thanks very much. I’m sorry if it comes across as unhelpful, but I’ve asked more than once whether you are storing the download count and just need to display it or whether you also need to learn how to store the count, and you haven’t answered. Your post comes across as if you want someone to do the work for you and just present a completed modification - I’m happy to help people learn how to do stuff (to the extent that my knowledge allows), but not to just sit down and write your code. So I’ll leave this one for someone else to help you.

1 Like

There is no database storing file information. It is strictly directory reading.

I’ve not looked at your code - I don’t wish to download something from Google Drive. However, from your earlier post it seems you have a database, so what is it used for?

It’s just a user table to log in.

1 Like

Well it’s not always an easy thing to get under the hood of someone else’s coding to see what’s what… but as you are not a coder I figured I’d give it a go. Well obviously the first thing is to create a database table to store the name of the file and keep a count of the downloads. This is the table I created.

CREATE TABLE IF NOT EXISTS `downloads` (  `id` int(11) NOT NULL AUTO_INCREMENT,
  `filename` varchar(256) DEFAULT NULL,
  `download_cnt` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Then it’s a matter of finding where downloading coding is. You will see it starts at around line 381 of file-gator.php. Now what we want to do is once we know the filename that is requested for download, we will query the downloads table to see if this filename is in the database table. Based on these findings we will either INSERT a new record or UPDATE the count of an existing record.
I will place these queries before the comment line `// Set headers

//ADDED Download CNT Query						
if(gatorconf::get('use_database')){												   

	$db = new DBDriver();
	$sql_download_cnt = "SELECT `download_cnt` FROM `downloads` WHERE `filename` = '{$filename}'"; 
	$result_cnt = $db->query($sql_download_cnt);
	$row_cnt = $result_cnt->fetch_assoc();
	if(count($row_cnt) <1){									  					
		$sql_downloads = "INSERT INTO `downloads`(`filename`, `download_cnt`) VALUES ('{$filename}',1)";		
		$db->execute($sql_downloads);
	}else{
		$sql_downloads = "UPDATE `downloads` SET `download_cnt`=`download_cnt`+1 WHERE `filename` = '{$filename}'";		
		$db->execute($sql_downloads);
	}				 
}		
	
// Set headers

Note: I am not going to modify your DB connection coding to use prepared queries.
Also note the use_datebase condition set on your configuration file needs to be set to true.

The second half of this is to query for all these file counts and compare them to the files being looped through. I placed the query at the top of the page called main_filelist.php and put the results into a $downloads_cnts array with the image name as the KEY and the count as the VALUE. I removed the DOT from the filename KEY.

$download_cnts = array();
$db = new DBDriver();
$sql_downloads = "SELECT `filename`,`download_cnt` FROM `downloads`"; 
$result_downloads = $db->query($sql_downloads);	
while($row = $result_downloads->fetch_assoc()){	
	$download_cnts[str_replace('.','',$row['filename'])] = $row['download_cnt'];
}
?>

As the page loops through the two loop sections, i.e. directories and files, I check if this filename is found in the $downloads_cnts array and set a variable $dcnt to the count or zero if not found.

<?php				
$cleanname = str_replace('.','',$file['name']);
$dcnt = (!empty($download_cnts) && array_key_exists($cleanname,$download_cnts) ? $download_cnts[$cleanname] : 0);
?>

I place this variable within the table structure below.

<td><?php echo $dcnt;?></td>

The only issue is that everything happens in a Javascript environment and so the page display is not updated as you view or download files. At any point you can click the LOGO and the page is reloaded showing the updated counts.

As main_filelist.php might be a bit tough for you to edit I uploaded my copy.
Good luck on your project.
main_filelist.zip (1.2 KB)

2 Likes

Thank you for the very helpful guide!
Thanks for your enthusiasm. Well worth a million likes!
What about when other people view the file, how can it show the views? What matters is the view, because in case someone else has the file’s path to share in many places, we have no control over the view.

The views are shown when the page is loaded. The download query is run so anyone visiting will see the latest count at that time. In your projects current form the directory structure of files and images can be seen by all who logged in. There is nothing personalized about it as your project as it is not database controlled. Your program just reads a directory and shows the items to everyone. So anyone looking at it will see the latest view at the time they load the page.