Jump to content

[SOLVED] only show certain files


jacko310592

Recommended Posts

i have the following code which shows all the file names of files on my webiste...

 

<?php
$files = new RecursiveDirectoryIterator("./");
foreach(new RecursiveIteratorIterator($files) as $files)
{
$files = basename($files);
echo ''.$files.'<br/>';
}
?>

 

 

my problem is that i only want this code to find files with a '#' at the begining of the files name, can anyone think of a way this code can be altered to do this.

 

thanks guys

Link to comment
https://forums.phpfreaks.com/topic/182045-solved-only-show-certain-files/
Share on other sites

just so you know newb, your example will work for file names with the character # anywhere, rather than strictly at the top

$files = array("#file1", "file2", "file3", "fil#", "#file5");
foreach($files as $files)
{
$files = basename($files);
if(preg_match('/#/', $files)) {
	echo ''.$files.'<br/>';
}
}

output:

#file1
fil#
#file5

 

you probably meant

foreach($files as $files)
{
$files = basename($files);
if (preg_match('/^[#]/', $files)){
	echo ''.$files.'<br/>';
}
}

 

the ^ part matches to the beginning of the string

thanks for the reply Ken2k7, but that code continues to show all the files on the site-

 

also thanks to you newbtophp for a reply, but i got an error message when i tried your code :/

 

but mikesta707, you code worked perfectly, thanks allot  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.