Forum Moderators: coopster

Message Too Old, No Replies

Extracting images only

Using scandir to extract images

         

technossomy

7:41 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



I have a directory on a site from which I want to extract the button images only for preloading purposes. These images start with "button.". I suppose I first use scandir('..'), but then how do I extract just those filenames that are buttons?

Thanks in advance

Salsa

7:59 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



How about just looping through all the file names, and:

if (preg_match('/^button\./',$filename)) { 
// add to an array or process them as you wish...
}

technossomy

8:25 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



Thanks for your prompt reply! I was already afraid that a regexp would be required.

Then what is the container for the regexp match? So when replacing 'if' by 'while' to get all the image names, the following should come closer:

$buttons = new array[]; // ..or some constructor
$files = scandir('..');
while (preg_match('/^button\./', $files)) { /* add imagename to array $buttons */ }

What goes in the comments?

Thanks again

technossomy

8:38 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



Hey Salsa

While it would still be useful to know how to get the regexp container for the match, the thread that is just following this one discusses the fact that PHP cannot do any client side scripting, which is exactly what I wanted it to do. So back to Javascript then, where I have no I/O functions, tough luck... Unless you know of some other option.

Best

Tech Nossomy

Salsa

9:13 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



I figured that you were planning to use this for a javascript preload images function, and while PHP is server side and JS is client side, there's no reason why you can't use PHP to write your JS code. So see if this doesn't work for you:

I've never used scandir because it's only available in PHP5, and I'm still using 4x. But if you're using PHP5 it looks like you could do something like:

$files = scandir('..'); 
$preload_images = "preloadImages("; //assumes preloadImages JS function
foreach($files AS $file) {
if (preg_match('/^button\./',$file)) $preload_images .= "'../$file',";
}
$preload_images = substr($preload_images,0,-1);
$preload_images .= ")";

...then echo that in your body tag, like: onload="<? echo $preload_images?>", which is where I think you want it.

I hope this helps.

technossomy

7:33 pm on Dec 29, 2004 (gmt 0)

10+ Year Member



Salsa

Thanks for this, I will get cracking with it today and report back if any problems encountered.

Best

Tech Nossomy