2013-03-29 32 views
0

我有一个直接链接到一个文件,我想给了我的观众,例如:直接链接访问但由mysql/php跟踪?

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

有什么办法,当上面的链接是平/打,我可以运行下面的查询/下载?

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

任何形式的帮助,我可以得到这是非常感谢。提前谢谢你。

+0

可能AJAX可以帮助你... – alwaysLearn

+1

如果他们直接进入mp3文件,ajax会如何帮助我? – thevoipman

回答

2

是的,这是可能的。你可以在apache中使用重写模块。所以你可以说所有(mp3)文件的服务器不应该返回mp3文件,而是运行一个php文件/脚本来执行你的查询并返回mp3文件。

这可能帮助你:http://www.workingwith.me.uk/articles/scripting/mod_rewrite

在.htaccess文件:

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

这将发送所有链接以.mp3结尾的index.php(actualy它仍然将是相同的链接,但在index.php将被执行)

其他选项,您有:

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

这将执行index.php与GET veriable文件名与文件名 例如。 $_GET['filename'] = "filename"(当文件:FILENAME.MP3)

在index.php文件,让用户下载的MP3文件(参见: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(); 

你可以这样做动态地用下面的代码:

$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(); 

,您可以访问请求的链接:

$_SERVER['REQUEST_URI']; 

或新的(生成的url带):

$_SERVER['REDIRECT_URL']; 
+0

这很接近,但我只能访问我自己的.htaccess ....任何考试我可以得到这种方法,我会感激。 – thevoipman

+0

看我的例子,虽然你应该知道它是如何工作的,或者它可能会危险使用它) –

+0

这是好的,但它运行index.php,但它不会让访客继续下载/流的MP3。 ... :( – thevoipman

0

是的,你可以这样做。点击url需要调用ajax来运行更新查询成功后,继续下载文件。

+0

这不会如果用户直接从网址栏访问mp3,则工作 – thevoipman

1

我认为还有另一种方式,你想要做什么:

  1. 创建一个新的PHP文件“下载”开始下载与参数 内容如下所示(日期和时间是关键,让我MP3文件):

    <?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. 创建一个不同的php文件“功能。PHP的”使用功能 “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. 使用不同的URL下载(无锚)

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