Php feof()和fread()产生错误

Php feof()和fread()产生错误,php,io,Php,Io,好吧,我在这个网站的某个地方发现了这个,我尝试了这个,但它只是在我的控制台上发了一大堆错误,我不知道我做错了什么 <?php set_time_limit(0); $dirPath = "masked on purpose"; $songCode = $_REQUEST['c']; $filePath = $dirPath . "/" . $songCode . ".mp3"; $bitrate = 128; $strContext=stream_context_create(

好吧,我在这个网站的某个地方发现了这个,我尝试了这个,但它只是在我的控制台上发了一大堆错误,我不知道我做错了什么

<?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);
    }
}
?>


代码工作正常,但问题是我希望流继续,即使没有人在侦听它,每次有人尝试侦听它时,它都会重新启动。

如果失败,
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);
    }
}

您没有检查对
fopen
的呼叫以确保它没有失败。启用错误报告以在测试时查看所有错误<代码>ini\U集合(“显示错误”,1);ini设置(“显示启动错误”,1);错误报告(E_全部)我试过调试代码,结果它试图找到一个名为“.mp3”的文件。由于我是PHP初学者,你能给我写一个如何读取文件夹中所有文件的例子吗?我不再收到错误,但我希望流独立运行,而不是每次有人访问它时都重新启动。我认为这将是一个不同问题的主题。