douqi1931 2014-05-16 17:08
浏览 33

Scandir函数将所有.php页面输出到网站 - PHP

I am trying to output all the .php files on my website to a page. I have managed to do it, to a certain extent.

I have managed to output all the files which are in my website directory. These include textfiles, image files, .php files, etc.

I just want the output to be .php files and for them to be hyper-linked.

This is currently my code:

<?php
$dir = '/home/website/allfiles';
$files = scandir($dir);
echo "<pre>";
print_r($files);
echo "</pre>";
?>

The code above just prints all the files (.txt, .jpeg, .php, etc) in a array listing.

  • 写回答

1条回答 默认 最新

  • duanqiao9541 2014-05-16 17:32
    关注

    To further my comment: You can make use of the glob() function (PHP 4 >= 4.3.0, PHP 5) and filter the file extensions you want to use/show.

    Using: GLOB_BRACE

    GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'

    Here is a basic example:

    <?php
    
    $directory = "images/"; // Use your preferred folder
    $files = glob($directory . '*.{jpg,jpeg,png,gif,doc,ppt}', GLOB_BRACE);
    
    foreach($files as $file)
    {
        echo '<a href="'.$file.'">'.$file.'</a>' . "<br>";
    }
    
    ?>
    

    If you don't want to use the filename as the shown text, you can use a general word, View file for example:

    Simply replace: swapping '.$file.' for View file

    echo '<a href="'.($file).'">'.$file.'</a>' . "<br>";
    

    with:

    echo '<a href="'.$file.'">View file</a>' . "<br>";
    

    Or using double quotes as the wrapped echo: (inverting the quotes for "'.$file.'")

    echo "<a href='".$file."'>View file</a>" . "<br>";
    

    To open the file in a new window/tab use target='_blank'

    Example:

    echo "<a href='".$file."' target='_blank'>View file</a>" . "<br>";
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致