dongyuan1160 2014-07-16 19:31
浏览 2995

PHP:警告:file_get_contents():文件名不能为空

I'm trying this new script on my server and I can't seem to figure out why it keeps giving me the error of:

Warning: file_get_contents(): Filename cannot be empty in /includes/classes/spin_article.php on line 51

This line is giving the error --> $string = file_get_contents($files[rand(1, count($files)) -1]);

<?PHP
class Spintax
{
    public function process($text)
    {
        return preg_replace_callback(
            '/\{(((?>[^\{\}]+)|(?R))*)\}/x',
            array($this, 'replace'),
            $text
        );
    }

    public function replace($text)
    {
        $text = $this->process($text[1]);
        $parts = explode('|', $text);
        return $parts[array_rand($parts)];
    }
}
?>

<?PHP
ob_start();
$files = glob("spintax_articles/*.txt");
$spintax = new Spintax();
$string = file_get_contents($files[rand(1, count($files)) -1]);
echo $spintax->process($string);
$page = ob_get_contents();
ob_end_flush();
$fp = fopen("content.txt","w");
fwrite($fp,$page);
fclose($fp);
?>
  • 写回答

1条回答 默认 最新

  • dqd82461 2014-07-16 19:39
    关注

    If there are actually matching files, then you may get a random index that is 1 greater than the maximum. Use:

    $string = file_get_contents($files[rand(1, count($files)-1)]);
    

    Notice the -1 placement.

    And just in case there is no files you should check for that, using a condition.

    <?php
    $files = glob("spintax_articles/*.txt");
    
    if(!empty($files)) {
    
        $spintax = new Spintax();
        $string  = file_get_contents($files[mt_rand(1, count($files)-1)]);
    
        $fp = fopen("content.txt", "w");
        fwrite($fp, $spintax->process($string));
        fclose($fp);
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目