简体   繁体   中英

how to get a list of files with .html extension using PHP scandir?

I am using the following code to get a list of files and folders in a directory. this works fine. but what I need to do is to only get a list of HTML files in that directory and ignoring the rest of the files and folders.

this is my code:

$path = "../";
$files = scandir($path);
foreach ($files as &$value) {
    echo "<a href='http://localhost/".$value."' target='_black' >".$value."</a><br/>";
}

I've tried this and still get all the files and folders:

$indir = scandir('../');
$fileextensions = array(".", "html");
$indir = array_diff($indir, array('.', '..'));
$replaceextensions = str_replace($fileextensions, "", $indir);

foreach ($indir as &$value) {
    echo "<a href='http://localhost/".$value."' target='_black' >".$value."</a><br/>";
}

could someone please advise on this?

Thanks

EDIT:

with the code bellow I can get the list of html files example( page1.html, page2.html etc). however, some of my html files have names like page1.tpl.html , page2.tpl.html etc and I cannot get those files in the list even though their extension is ending with .html .

could someone please advise on this?

this is my code now:

$path = "../";
$files = scandir($path);
$files = glob("*.{html}", GLOB_BRACE);
foreach ($files as &$value) {
    echo "<a href='http://localhost/".$value."' target='_black' >".$value."</a><br/>";
}

This worked for me:

$path = "../";
$files = scandir($path);
$files = glob("*.{html}", GLOB_BRACE);
foreach ($files as &$value) {
    echo "<a href='http://localhost/".$value."' target='_black' >".$value."</a><br/>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM