首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >feof()和fread()产生错误

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

Stack Overflow用户
提问于 2018-08-21 16:13:54
回答 1查看 50关注 0票数 0

好的,我在这个网站上找到了这个,我试了一下,但它只是在发垃圾邮件给我的控制台带来大量错误,我不明白我做错了什么

代码语言:javascript
复制
<?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文件

这里的编辑:我已经将脚本更改为如下所示

代码语言:javascript
复制
<?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);
    }
}
?>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 16:20:53

fopen函数将向打开的文件返回一个资源,如果失败,则返回一个FALSE布尔值。看来你的文件没能打开。检查$filePath是否正确,$songCode是否有一个值。

下面是读取文件夹中所有文件的代码:

代码语言:javascript
复制
// 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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51952817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档