好的,我在这个网站上找到了这个,我试了一下,但它只是在发垃圾邮件给我的控制台带来大量错误,我不明白我做错了什么
<?php
set_time_limit(0);
$dirPath = "masked on purpose";
$songCode = $_REQUEST['c'];
$filePath = $dirPath . "/" . $songCode . ".mp3";
$bitrate = 128;
$strContext=stream_context_create(
array(
'http'=>array(
'method'=>'GET',
'header'=>"Accept-language: en\r\n"
)
)
);
header('Content-type: audio/mpeg');
header ("Content-Transfer-Encoding: binary");
header ("Pragma: no-cache");
header ("icy-br: " . $bitrate);
$fpOrigin=fopen($filePath, 'rb', false, $strContext);
while(!feof($fpOrigin)){
$buffer=fread($fpOrigin, 4096);
echo $buffer;
flush();
}
fclose($fpOrigin);
?>

我想要做的是制作一个在线广播流,扫描一个文件夹,并循环其中的所有.mp3文件
这里的编辑:我已经将脚本更改为如下所示
<?php
set_time_limit(0);
$dirPath = "...";
$bitrate = 128;
$strContext=stream_context_create(
array(
'http'=>array(
'method'=>'GET',
'header'=>"Accept-language: en\r\n"
)
)
);
header('Content-type: audio/mpeg');
header ("Content-Transfer-Encoding: binary");
header ("Pragma: no-cache");
header ("icy-br: " . $bitrate);
$list = scandir($dirPath);
foreach($list as $file)
{
if($file== '.' or $file== '..')
continue; // skip, not a file or a folder
if(is_dir($file))
continue; // skip, not a file
echo $file . "<br>";
// define the file path
$filePath = $dirPath . '/' . $file;
// read the file
$fh = fopen($filePath, "r") or die("Could not open file.");
if ($fh) {
while (!feof($fh)) {
$buffer = fgets($fh, 4096);
echo $buffer;
flush();
}
fclose($fh);
}
}
?>代码工作正常,但问题是,我希望流继续,即使没有人听它,它重新启动,每次有人试图听它。
发布于 2018-08-21 16:20:53
fopen函数将向打开的文件返回一个资源,如果失败,则返回一个FALSE布尔值。看来你的文件没能打开。检查$filePath是否正确,$songCode是否有一个值。
下面是读取文件夹中所有文件的代码:
// get a list of all files/folders in a path
$list = scandir($dirPath);
foreach($list as $file)
{
if($file== '.' or $file== '..')
continue; // skip, not a file or a folder
if(is_dir($file))
continue; // skip, not a file
// define the file path
$filePath = $dirPath . '/' . $file;
// read the file
$fh = fopen($filePath, "r") or die("Could not open file.");
if ($fh) {
while (!feof($fh)) {
$buffer = fgets($fh, 4096);
// Do something with the buffer here...
}
fclose($fh);
}
}https://stackoverflow.com/questions/51952817
复制相似问题