duanleiliu7373 2014-11-17 21:07
浏览 20
已采纳

显示.php文件的内容而不是数组中的变量名称

I'm trying to make a simple news feed on my site that will show the latest 3 posts from a directory.

I have a directory full of .php files, inside each is some html and a short story with a link to the full page.

On my home page I've got

<?php
$news = scandir('news_posts', SCANDIR_SORT_DESCENDING);
include($news[0]);
include($news[1]);
include($news[2]);
?>

Which isn't working. I've tried print_r, var_export, echo, the best I've managed to do is have it print the names of the variables (so 14.11.14.php, 15.11,14.php, etc.) rather than the html content within the .php files.

This is probably really simple, but being new to .php I'm a little lost and can't seem to find an appropriate answer via search engines.

  • 写回答

1条回答 默认 最新

  • douju9272 2014-11-17 21:09
    关注

    scandir only returns file names not the path, which seems to be news_posts/. Try glob:

    $news = glob('news_posts/*.*');
    

    Or to keep using scandir:

    include('news_posts/' . $news[0]);
    

    I would use glob since you can return only files and only files with a specific extension.

    sort($news, SORT_DESC);
    
    foreach($news as $file) {
        include($file);
    }
    

    To glob and sort by time in one go:

    array_multisort(array_map('filemtime', $news = glob('news_posts/*.*')), 
                    SORT_DESC, $news);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答