Hi all,

I have a file download counter on my website, here, that I am playing around with.

The files are stored in a folder on my server and their filenames are used to populate a mysql database.

What I would dearly like is for the filename without it's extension to be displayed in the table on my webpage but I can't seem to figure it out.

I have tried a few methods but have not got anywhere.

I can't seem to get anything to work when I replace the $val variable in the href statement.

I would be grateful for any pointers here.

Many thanks..,

K.

<?php

// Error reporting:
error_reporting(E_ALL^E_NOTICE);

// Including the DB connection file:
require 'dlc_connect.php';

$extension='';
$files_array = array();


/* Opening the thumbnail directory and looping through all the thumbs: */

$dir_handle = @opendir($directory) or die("There is an error with your file directory!");

while ($file = readdir($dir_handle)) 
{
	/* Skipping the system files: */
	if($file{0}=='.') continue;
	
	/* end() returns the last element of the array generated by the explode() function: */
	$extension = strtolower(end(explode('.',$file)));
	
	/* Skipping the php files: */
	if($extension == 'php') continue;

	$files_array[]=$file;
}

/* Sorting the files alphabetically */
sort($files_array,SORT_STRING);

$file_downloads=array();

$result = mysql_query("SELECT * FROM download_manager");

if(mysql_num_rows($result))
while($row=mysql_fetch_assoc($result))
{
	/* 	The key of the $file_downloads array will be the name of the file,
		and will contain the number of downloads: */
		
	$file_downloads[$row['filename']]=$row['downloads'];
}

?>

<link rel="stylesheet" type="text/css" href="****.css" />
<script type="text/javascript" src="****.js"></script>
<script type="text/javascript" src="****.js"></script>

<center>
<table>
<tr>
<td>File Name</td>
<td>Downloads</td>
</tr>
 <?php 

        foreach($files_array as $key=>$val)
        {
            echo '
<tr>
<td><a href="dlc_download.php?file='.urlencode($val).'">'.$val.'</a></td> 
<td><span class="download-count">'.(int)$file_downloads[$val].'</span></td>
</tr>';
        }
    
    ?>

</table>
</center>

You need the extension in the table or file handling wont work, you dont need it displayed in the download link

<?php   foreach($files_array as $key=>$val)  {            echo '<tr><td><a href="dlc_download.php?file='.urlencode($val).'">'.substr($val, 0, strlen($val -4)).'</a></td> 
<td><span class="download-count">'.(int)$file_downloads[$val].'</span></td>;
</tr>'; }  ?>
commented: Excellent job, thank you! +1

You need the extension in the table or file handling wont work, you dont need it displayed in the download link

<?php   foreach($files_array as $key=>$val)  {            echo '<tr><td><a href="dlc_download.php?file='.urlencode($val).'">'.substr($val, 0, strlen($val -4)).'</a></td> 
<td><span class="download-count">'.(int)$file_downloads[$val].'</span></td>;
</tr>'; }  ?>

Thanks almostbob, that is exactly what I was after I just couldn't get my head round it.

I have changed it around a little using strpos as you can see below, this should work fine as long as I keep '.' out of the filenames.

Thank you so much for your help..,

K

<?php 

        foreach($files_array as $key=>$val)
        {
            echo '
<tr>
<td><a href="http://www.madtogger.co.uk/dlc_download.php?file='.urlencode($val).'">'.substr($val, 0, strpos($val, '.')).'</a></td> 
<td><span class="download-count">'.(int)$file_downloads[$val].'</span></td>
</tr>';
        }
    
    ?>

Firstly, where is the $directory variable coming from? As for your original question, take a look at php.net/substr and php.net/strpos

Thanks for replying ShawnCplus, the $directory variable is called from another php file.

Thanks to your help pointing me to strpos & the help from almostbob, I have it sorted now.

Great stuff..,

K

Hi,

it's me back again.

I have found a flaw in my Download Counter, not totally serious but really more cosmetic actually.

When a user clicks the file link to download, the file automatically downloads and the counter increases by 1 as it should. This is done via a js script using the click function and relates to when the user clicks the Table Row, <tr> tag.

js script below:-

$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	$('tr').click(function(){
		
		var countSpan = $('.download-count',this);
		countSpan.text( parseInt(countSpan.text())+1);
	});
});

Now that is all fine but the problem is that the counter will also increase by one every time the user clicks on cell2 of the row although the download will not start in this scenario and because of this the counter will return to it's previous state on page refresh.

I thought a way around this was to call the click funtion from the <td> tag, which does work for downloading the file but the counter will not automatically increase until the page is refreshed.

Really what I am trying to do is isolate the download link so only clicking this will increase the counter and make it so no action takes place when the user clicks cell2.

Hope this makes sense.

Regards..,

K

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.