$array = Array( 0 => '-1.jpg', 1 => '-2.jpg', 2 => '-3.jpg', 3 => '-4.jpg', 4 => '-5.jpg', 5 => '-6.jpg', 6 => '-7.jpg', 7 => '-8.jpg', 8 => '-9.jpg', 9 => '-10.jpg', 10 => '-11.jpg', 11 => '-12.jpg', 12 => '-13.jpg', 13 => '-14.jpg', 14 => '-15.jpg', 15 => '-16.jpg', 16 => '-17.jpg', 17 => '-18.jpg', 18 => '-19.jpg', 19 => '-20.jpg', 20 => '-30.jpg' ); if ($handle = opendir('/skin-batcher/omgz')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $filename = '/skin-batcher/omgz/$file'; $id = time( ); // filename foreach ( $array as $needle ) { if (strpos($file,$needle) !== false) { echo $file; } } } } closedir($handle); } ?> PHP: I have over 5000 files in a directory. and if they contain "-XXX.jpg" I want to do something with them. The above code.. kind of works but not quite accurately. is there any easier way .. a way that will definitely work.
What do you want to do with them? Noone here owns a crystal ball. In what way does it "kind of" work? In what way does it not work "quite accurately"? An easier way to accomplish what? A way that would work doing what? Again...noone owns a crystal ball. Your post is so vague it's absolutely pointless to TRY and see what you are doing with your code.
@Netstar, I think what the OP meant is that he wants all of his files that contains "-XXX.jpg" to be referred to so that I can do something with it.
Not up to anyone here to guess what he wants... or to assume. He needs to clarify what he's trying to do, why his code doesn't work for him, and what he would like to accomplish.
Hey, this could do it if ($handle = opendir('/skin-batcher/omgz')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $filename = '/skin-batcher/omgz/$file'; $id = time( ); // filename if (preg_match('/-([0-9]+)\.jpg$/', $file)) { echo $file; } } } closedir($handle); } ?> PHP:
A simple regular expression can match the end of the filename, cheers! <?php $dir = 'dir/'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (preg_match('#-[0-9]+\.jpg$#', $file)) { // do something with $file } } } closedir($handle); } ?> PHP: