0

I have a direct link to a file I want to give out to my visitors, for example:

http://www.mydomain.com/mynewmix.mp3

Is there any way I can run the following query when the link above is being ping/hit/downloaded?

UPDATE `db` SET `downloaded`=`downloaded`+1 WHERE `url`='http://www.mydomain.com/mynewmix.mp3'

Any kind of help I can get on this is greatly appreciated. Thank you so much in advance.

thevoipman
  • 1,773
  • 2
  • 17
  • 44

3 Answers3

2

Yes this is possible. you can use rewrite module in apache. so you can say that for all (mp3) files the server shouldn't return the mp3 file but instead run a php file/script which executes your query and returns the mp3 file.

this might help you: http://www.workingwith.me.uk/articles/scripting/mod_rewrite

in your .htaccess file:

RewriteEngine on
RewriteRule ^\.mp3$ index.php [L]

this will send all links ending with .mp3 to index.php (actualy it will still be the same link but the index.php will be executed)

other option you have:

RewriteRule ^.*/(\w)*\.mp3$ index.php?filename=$1 [L] 

this will execute index.php with the GET veriable filename with the filename eg. $_GET['filename'] = "filename" (when file: filename.mp3)

in index.php to let the user download the mp3 file (see: Can I serve MP3 files with PHP?):

header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize("[file location eg: /home/files/sometrack.mp3]"));
header('Content-Disposition: attachment; filename="sometrack.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile("[file location eg: /home/files/sometrack.mp3]");
exit();

you could do this dynamicly with the following code:

$fileName = $_GET['filename']."mp3"; //we stripped .mp3 in the apache rewrite (might not be so smart)
$fileLocation = "/your/location/".$filename;
header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($fileLocation));
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($fileLocation);
exit();

you can access the requested link with:

$_SERVER['REQUEST_URI'];

or the new (generated url with):

$_SERVER['REDIRECT_URL'];
Community
  • 1
  • 1
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
  • this is close, but I only have access to my own .htaccess.... any examples I can get with this method would be appreciated. – thevoipman Mar 29 '13 at 09:44
  • see my example, though you should find out how it works yourself, or it can be dangerous using it ;) – Joel Harkes Mar 29 '13 at 09:49
  • this is good, but it runs index.php but it doesn't let visitors continue to download/stream that mp3.... :( – thevoipman Mar 29 '13 at 09:55
  • 1
    your answer is here: http://stackoverflow.com/questions/1516661/can-i-serve-mp3-files-with-php – Joel Harkes Mar 29 '13 at 09:59
  • Thanks Joel... I'm doing this as well: RewriteRule ^.*/(\w)*\.mp3$ index.php?filename=$1 [L] and when I echo out the GET, it's not giving me the complete link in the url bar... – thevoipman Mar 29 '13 at 10:09
  • sometrack.mp3 above, can it be replaced with: http://www.mydomain.com/mynewmix.mp3 ??? – thevoipman Mar 29 '13 at 10:15
  • sometrack.mp3 is just the file name which gets downloaded – Joel Harkes Mar 29 '13 at 10:20
  • i forgot to add some code but you need the local file location if you work in php, you cant acceess the external link or else you get an infinite loop because the server will redirect the link to index.php ;) – Joel Harkes Mar 29 '13 at 10:23
  • darn it, seems like this will kill the ability to stream the mp3 from a flash player... it will let me download just fine, but not streaming :( – thevoipman Mar 29 '13 at 10:39
  • im pretty sure the header files can be changed to let you stream it? remove 'attachement' should work? – Joel Harkes Mar 29 '13 at 10:41
  • still downloads like a charm :) can't stream, flashplayer say file not found :( – thevoipman Mar 29 '13 at 10:46
  • Are you sure there's no way I can just have .htaccess execute a php file without touching the mp3 file at all? – thevoipman Mar 29 '13 at 10:52
1

I think there is another way that does what you want:

  1. Create a new php file "download" to start download with parameters Contents look like this (date and time are keys to get my mp3-file):

    <?php
        if (isset($_GET['date']) && is_string($_GET['date']) && isset($_GET['time']) && is_string($_GET['time']))
        {
            require_once($_SERVER['DOCUMENT_ROOT'] . "/bin/functions.php");
            DownloadFile($_GET['date'],$_GET['time']);
        }
    ?>
    
  2. Create a different php file "functions.php" with the function "DownloadFile":

    function DownloadFile($Date,$Time)
    {
        require('connection.php');
        mysql_select_db($database, $instance);
    
        $qry = "
            UPDATE `table` 
            SET `Field1` = `Field1` + 1
            WHERE `Field2` = '$Date' AND `Field3` = '$Time'
            ";
        $result = mysql_query($qry, $instance) or die(mysql_error());
    
        header('Content-type: Application/mp3');
        header("Content-Length: " .(string)(filesize($file)));
        header('Content-Disposition: attachment; filename=' . $file);
        readfile($file);    
    

    }

  3. Use different url for downloading (no anchor)

http://www.yoursite.com/xxx/download.php?date=2000-01-01&time=12:00:00

Polleke
  • 11
  • 1
0

Yes you can do this. on click of url need to call ajax to run update query on success you proceed to download the file.

Praveen kalal
  • 2,148
  • 4
  • 19
  • 33