skipping unwanted files with readdir

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

skipping unwanted files with readdir

Post by mlecho »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi guys...got a q....here is the code

Code: Select all

$menuType=$_GET['menuType'];
$directory="images/$menuType";

$openDir=opendir($directory);

while(false!=($file=readdir($openDir))){
	if($file !="." && $file !=".." && $file !=".DS_Store" && $file !="._.DS_Store"){
		$files.= "<pict>$file</pict>";
		}
		
}

as you can see, i work mostly on a mac...the .DS_Store and ._.DS_Store are not read by the PHP (yeah!)...however, i still get files such as ._0.jpg, ._1.jpg, etc...what am i missing. SHouldn't the $file !="." prevent that?


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Have a look at preg_match
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Post by mlecho »

hi jamiel...really, i am using my brain here, and it is starting to hurt....i understand what the preg_match does, but not sure how to use it in my particular place.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

to clarify, you do not want files prefixed with '.' (fullstop) to be shown, i.e. hidden files on max/*nix?

This should work:

Code: Select all

<?php

$handle = opendir($directory)

while (($file = readdir($director)) !== false) {
    if (!preg_match('/^\./', $file)) {
        echo "\$file = $file";
    }
}

?>
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Post by mlecho »

oh oh...i see i have some shorthand and operator syntax to learn...any clues on where to start with this...besides the php manual...

why the slash on the echo?

Code: Select all

echo "\$file = $file";
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

mlecho wrote:oh oh...i see i have some shorthand and operator syntax to learn...any clues on where to start with this...besides the php manual...

why the slash on the echo?

Code: Select all

echo "\$file = $file";
It is an escape character.

e.g.

Code: Select all

$file = "Hello";
echo "$file = $file";
// Outputs: Hello = Hello

Code: Select all

$file = "Hello";
echo "\$file = $file";
// Outputs: $file = Hello
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Post by mlecho »

thanks pimptastic....and you are right, php manual is my friend...i will use it.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

All you should do is verify if the first character of the http://www.php.net/string equals a dot (read the 'String access and modification by character' section). With http://www.php.net/strpos you can also determine the position of a dot in a given string... Imho, there are easy (presuming that regulare expressions aren't easy) options too....
Post Reply