你有一些選擇,
輸出的文件路徑正確(最簡單的)
生成並存儲在數據庫或平面文件的MP3的註冊和使用mod_rewrite通過加載器腳本的參數。 (下面的示例)
<?php
/* mod_rewrite .htaccess in chapters folder
RewriteEngine On
Options -Indexes
RewriteBase /chapter1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).mp3$ index.php?loadmp3=$1 [L]
*/
$mp3_dir='./mp3s/';
//Example passed url: http://localhost/chapter1/book.002.mp3
if(isset($_GET['loadmp3'])){
$real_path = get_mp3_list($_GET['loadmp3']);
if($real_path != false){
//Result: ./mp3s/book-next_random_name.002.mp3
print_r($real_path);
//Pass the url to a streamer script
}else{
//not found
}
}
/**
* Build & cache, search for mp3 from array
*/
function get_mp3_list($search){
global $mp3_dir;
if(file_exists($mp3_dir.'mp3s.json')){
$list = json_decode(file_get_contents($mp3_dir.'mp3s.json'),true);
}else{
$mp3s = glob($mp3_dir."*.mp3");
$list = array();
foreach($mp3s as $mp3){
if(preg_match("#(\w+)-(\w+).(\d+).mp3#", $mp3, $match)){
$list[]=array('location'=>$mp3,
'type'=>$match[1],
'name'=>$match[2],
'episode'=>$match[3]);
}
if(!empty($list)){file_put_contents($mp3_dir.'mp3s.json', json_encode($list));}
}
}
$search = explode('.',$search,2);
foreach($list as $mp3){
if($mp3['type'] == $search[0] && $mp3['episode'] == $search[1]){
return $mp3['location'];
}
}
return false;
}
/*
$list Example Array
(
[0] => Array
(
[location] => ./mp3s/book-next_random_name.002.mp3
[type] => book
[name] => next_random_name
[episode] => 002
)
[1] => Array
(
[location] => ./mp3s/book-some_random_name.001.mp3
[type] => book
[name] => some_random_name
[episode] => 001
)
)
*/
?>
希望它可以幫助
大多數網絡服務器往往依據是你把他們的鏈接指向的實際文件上工作。你想自動生成這些鏈接?查看「DirectoryIterator」甚至「glob」。如果你手動完成,然後咬住子彈並自己輸入正確的文件名,或者重命名你的文件以匹配網站。 – 2012-08-13 04:34:17