Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.