Forum Moderators: coopster

Message Too Old, No Replies

extention

easy one

         

WhosAWhata

2:12 am on Jan 24, 2004 (gmt 0)

10+ Year Member



how can you search a dir for only one file extention
<?
$n = 1;
if (is_dir(catalogs/$cat)) {
$dir = "products/";
$dh = opendir($dir);
while (false!== ($filename = readdir($dh))) {
if (($filename!= ".") AND ($filename!= "..")) {
// here is where my code goes
// if $filename's extention ==.php then.....

}
}
}
?>

coopster

3:01 am on Jan 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are a couple of ways. One is to check the last four characters of the filename...

if ($filename!= "." AND $filename!= ".." and substr($filename, -4) != '.xyz') {

...or, alternatively, you could use PHP's pathinfo() [php.net] function.

WhosAWhata

3:29 am on Jan 24, 2004 (gmt 0)

10+ Year Member



thanks...once again you've saved my life...or at least my site (same thing)

BitBanger

2:57 pm on Jan 24, 2004 (gmt 0)

10+ Year Member



Another possibly easier solution is to use glob().

foreach (glob("*.php") as $filename) {
echo "$filename<br />";
}

coopster

3:47 pm on Jan 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's cool, BitBanger. Thanks for the tip!

ergophobe

4:22 pm on Jan 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




glob
(PHP 4 >= 4.3.0)

Geez! Gotta reread the manual with every release (not that I'm complaining, mind you, except perhaps about my own ignorance).

Maybe PHP 9 will finally have the holy grail, the

do_what_I_want_not_what_say()

function and then they can just stop adding stuff.

Tom

coopster

11:42 pm on Jan 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I hear ya, ergophobe. I guess we just have to follow the advice of one jatar_k [webmasterworld.com],

I have gotten into reading the changefile for new releases of php too. I can't tell you how many times I have found a function that I have had to custom build in the past.

Sometimes when I have a little time I go through the functions in the manual to see if I can find something exciting or to just see how I could use some of the functions that I don't normally play with.

BitBanger

2:22 am on Jan 26, 2004 (gmt 0)

10+ Year Member



Strangely enough, I just had to do the opposite. I found a neat function called array_combine() that exactly fit the bill. I coded it into a script and got a php error 'function array_combine() not found'. Seems I didn't look quite close enough at the function documentation as it is a PHP5 function.

I ended up coding my own. :(

WhosAWhata

3:21 am on Jan 26, 2004 (gmt 0)

10+ Year Member



personally i've found that coding my own functions is helpful, but very time consuming
i always end up spending so much time fixing errors that it seems like its almost not worth it