2012-03-10 40 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>