NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP - Reading/displaying files from directory (help!)

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 11-24-2005, 10:37 AM THREAD STARTER               #1 (permalink)
NamePros Regular
 
Virgil's Avatar
Join Date: May 2004
Posts: 958
Virgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to all
 



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


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 ).

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 .
Virgil is offline  
Old 11-24-2005, 11:12 AM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Here is the code you can use:-

The function itself

????: NamePros.com http://www.namepros.com/programming/142592-php-reading-displaying-files-directory-help.html
PHP Code:
<?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 Code:
<?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 by Peter; 11-24-2005 at 11:27 AM.
Peter is offline  
Old 11-24-2005, 11:22 AM   #3 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Not totally sure on this, but you could possibly try something like this:

PHP Code:
<?php

if($handle opendir('/path/to/directory/'))
{
  while(
false !== ($files readdir($handle)))
  {
    if(
$files != "." && $files != "..")
    {
      if(
stristr("-article.php"$files))
      {
        
sort($files);
????: NamePros.com http://www.namepros.com/showthread.php?t=142592
        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 by SecondVersion; 11-24-2005 at 11:26 AM.
Eric is offline  
Old 11-24-2005, 11:24 AM THREAD STARTER               #4 (permalink)
NamePros Regular
 
Virgil's Avatar
Join Date: May 2004
Posts: 958
Virgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to allVirgil is a name known to all
 



Thanks a lot, filth & SecondVersion. I'm trying to create a very simple 'article wrapper', and this will realy help me. Rep. added.
Virgil is offline  
Old 11-24-2005, 11:26 AM   #5 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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
Peter is offline  
Old 11-24-2005, 11:26 AM   #6 (permalink)
Eating Pie
 
iNod's Avatar
Join Date: Nov 2004
Location: Canada
Posts: 2,267
iNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud of
 


Special Olympics AIDS/HIV Cystic Fibrosis Save The Children Baby Health Cystic Fibrosis
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.

Originally Posted by filth
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.
????: NamePros.com http://www.namepros.com/showthread.php?t=142592

iNod.
__________________
I feel old.
iNod is offline  
Old 11-24-2005, 11:28 AM   #7 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by filth
just a note secondversion but not really any need for the following line:-
????: NamePros.com http://www.namepros.com/showthread.php?t=142592

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
Eric is offline  
Old 11-24-2005, 11:33 AM   #8 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Originally Posted by iNod
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 by Peter; 11-24-2005 at 11:39 AM.
Peter is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOWTO: Install the Apache Web Server, Perl, PHP, and MySQL on Windows deadserious Webmaster Tutorials 96 05-27-2007 01:24 PM
Greatest Image Gallery Tutorial Ever Wybe Webmaster Tutorials 5 12-20-2005 10:51 AM
Googlism - What does google think of you? deadserious The Break Room 55 12-15-2005 09:09 AM
PHP includes refererencing files from different directory thread FreeBaGeL Programming 6 03-08-2005 12:10 PM

 
All times are GMT -7. The time now is 02:03 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger