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'];