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.

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>";