2012-03-10 38 views
0

我已经上传了一些样品MP3文件的httpdocs以外的目录,我保证,这是访问通过正确配置的open_basedir到PHP和测试该目录工作。流保护的媒体(位于httpdocs资料外)与jPlayer

我想这样做是通过PHP文件流这些文件未通过认证的用户不应该访问这些文件。我目前正在使用jPlayer,并期望setMedia功能应该类似于此:

$("#jquery_jplayer").jPlayer("setMedia", { mp3: "stream.php?track=" + id + ".mp3" }); 

我已经尝试设置内容头部等在stream.php,它目前看起来是这样的:

$filePath = "../song_files/mp3/"; 
$fileName = "$_GET[track].mp3"; 

header("Content-Type: audio/mpeg"); 
header('Content-Disposition: attachment; filename="'.$fileName.'"'); 

getFile($filePath + $fileName); 

如果我直接加载这个页面,MP3文件下载并播放罚款,但是当我使用以上的JavaScript,jPlayer不播放的曲目。

我看过这篇文章(Streaming an MP3 on stdout to Jplayer using PHP),它似乎是用户试图达到我想要的,但是在测试解决方案后,我一直遇到问题,我得到的是“卷曲失败”。

是否有任何不同的方法,我可以用它来实现这一目标。指点我在正确的方向将不胜感激。

回答

0

各地的一些我发现,是工作的罚款解决搜索后。我使用的代码从类似主题(PHP to protect PDF and DOC

我将会把我这里使用的代码,以帮助回答正确的问题:

//check users is loged in and valid for download if not redirect them out 
// YOU NEED TO ADD CODE HERE FOR THAT CHECK 
// array of support file types for download script and there mimetype 
$mimeTypes = array(
    'doc' => 'application/msword', 
    'pdf' => 'application/pdf', 
); 
// set the file here (best of using a $_GET[]) 
$file = "../documents/file.doc"; 

// gets the extension of the file to be loaded for searching array above 
$ext = explode('.', $file); 
$ext = end($ext); 

// gets the file name to send to the browser to force download of file 
$fileName = explode("/", $file); 
$fileName = end($fileName); 

// opens the file for reading and sends headers to browser 
$fp = fopen($file,"r") ; 
header("Content-Type: ".$mimeTypes[$ext]); 
header('Content-Disposition: attachment; filename="'.$fileName.'"'); 

// reads file and send the raw code to browser  
while (! feof($fp)) { 
    $buff = fread($fp,4096); 
    echo $buff; 
} 
// closes file after whe have finished reading it 
fclose($fp); 
</code></pre>