LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
The function fnmatch() helps:
PHP Code:
<?php

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

?>

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

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 PM.

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