NameSilo

PHP - Reading/displaying files from directory (help!)

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
Watch

Virgil

VIP Member
Impact
22
Hi,

I'm a newbie at PHP programming, and I'm trying to figure out an easy way to do the following (I usually end up writing lots of code for things that could take a few lines :hehe: ).

I want a code snippet/function that reads files in a directory that:

A) Have .php extension
B) End with -article

IE:

motor-article.php

and then display them in alphabetical order, like:

autos-article.php
beauty-article.php

What's the easiest/shortest way to accomplish this?.

Thanks for your help :).
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Here is the code you can use:-

The function itself

PHP:
<?PHP
function get_wanted_files($preg, $folder)
{
	$matched = array();
	if ($folder_content = opendir($folder))
	{
		while (false !== ($file = readdir($folder_content)))
		{
			if (preg_match($preg, $file, $rubbish))
			{
				$matched[] = $file;
			}
		}
		sort($matched);
	}
	closedir($folder_content);
	if (!empty($matched))
	{
		return $matched;
	}
	else 
	{
		return false;
	}
}
?>

And it's usage

PHP:
<?php
$folder = '/home/flexiscr/public_html/test/config';
$needed = '/-article.php/';
if (get_wanted_files($needed, $folder))
{
	foreach (get_wanted_files($needed, $folder) as $file)
	{
		echo $file.'<br>';
	}
}
else
{
	echo 'No files found';
}
?>
 
Last edited:
1
Close
Not totally sure on this, but you could possibly try something like this:

PHP:
<?php

if($handle = opendir('/path/to/directory/'))
{
  while(false !== ($files = readdir($handle)))
  {
    if($files != "." && $files != "..")
    {
      if(stristr("-article.php", $files))
      {
        sort($files);
        foreach($files as $file)
        {
          echo $file."<br>";
        }
      }
    }
  }
  closedir($handle);
}

?>

EDIT: Filth beat me, heh. That'll work. As for the above, I'm not totally sure. You can try both, whichever works...heh
 
Last edited:
1
Close
Thanks a lot, filth & SecondVersion. I'm trying to create a very simple 'article wrapper', and this will realy help me. Rep. added.
 
0
Close
just a note secondversion but not really any need for the following line:-

if($files != "." && $files != "..")

that is addressed in the next if statement as they do not meet that if statement anyway.

ooh and just noticed when looking at your code I forgot to close the handle
 
0
Close
Both of those above will work. I recommand SecondVersions because if you got like 7000 files you will want less amount of php code. For fliths it sets it than it parses it. Which would create some slowness when doing high amounts. SecondVersions will just parse it.

iNod.

filth said:
just a note secondversion but not really any need for the following line:-

if($files != "." && $files != "..")

that is addressed in the next if statement as they do not meet that if statement anyway.

ooh and just noticed when looking at your code I forgot to close the handle

There is no need to bark at him, lots of people usually miss { and } while typing it up on Namepros. The more error checking you got the better. But for mass amounts of files the less you got is better.

iNod.
 
1
Close
filth said:
just a note secondversion but not really any need for the following line:-

if($files != "." && $files != "..")

that is addressed in the next if statement as they do not meet that if statement anyway.

ooh and just noticed when looking at your code I forgot to close the handle
As I said, wasn't totally sure on it. :) And no problem virgil :)
 
0
Close
iNod said:
There is no need to bark at him, lots of people usually miss { and } while typing it up on Namepros. The more error checking you got the better. But for mass amounts of files the less you got is better.

What do you mean bark at him!!!

I was simply pointing it out simple as that. And you have completely missed the point of the post. It had nothing to do with the curly braces. I was telling him he had no need to check if the file name was . or .. as the second if statement check alread what the file name is and both of those will not match the second statement.

If you read his code you will actuially see that he has in fact not left them out I just copied and pasted the single line so he knew which 1 I was on about
 
Last edited:
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
CryptoExchange.com
Domain Recover

NamePros updates

Quick replies: search, your most used replies, and more on every post
Quick replies are easier to find and use now. The quick reply menu has a search box. Type a few letters to find the reply you want. A...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back