Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-29-2005, 07:46 AM   #1
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Rep: Reputation: 40
PHP: List files in directory, exclude backups

I'm writing a PHP script which is to (amongst others) iterate a directory and list the files contained within it. However, currently all files are listed, including backups (those ending with "~") and working files (starting and ending with "#"). Is there an easy way to exclude these files, or do I have to test for file endings when deciding to display the file or not?
Old 12-29-2005, 12:00 PM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 109Reputation: 109
The function fnmatch() helps:
PHP Code:

$dh opendir($dir) or die("Cannot open dir $dir");
    while ((
$entry readdir($dh))) {
        if (
is_file($entry) && !fnmatch('*~'$entry) && !fnmatch('#*#'$entry)) {
$entry ."<br />";


Last edited by Hko; 12-29-2005 at 12:05 PM.
Old 12-30-2005, 04:27 AM   #3
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
Thanks, I'll go for that solution.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ List Files In Directory bendeco13 Programming 8 11-02-2010 12:08 PM
how to list how many files in a directory? malaka56 Linux - Software 8 09-02-2005 05:37 AM
rsync exclude list steelhead79 Linux - Software 3 01-14-2005 12:10 PM
How to list all the files in a directory Linh Programming 2 05-11-2004 10:09 AM
list only files in current directory xscousr Programming 8 09-22-2003 07:35 AM

All times are GMT -5. The time now is 01:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration