Jump to content

[SOLVED] display both jpg and gif file from directory....pls help


paparanch

Recommended Posts

hello gurus!

 

i have here a code which i display images from a folder.

but the problem is only gif files were displayed..so how am i going to display both jpg and gif files from this code?

please help...tnx

 

<?php
//This will get an array of all the .gif images in a folder
$img_array = glob("/path/to/images/*.gif");
//Pick a random image from the array
$img = array_rand($img_array);
//Display the image on the page
echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" />';
?> 

<?php
//This will get an array of all the .gif images in a folder
$img_array = glob("/path/to/images/*.gif");
//Pick a random image from the array
$img = array_rand($img_array);
//Display the image on the page
echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" />';

$img_array = glob("/path/to/images/*.jpg");
//Pick a random image from the array
$img = array_rand($img_array);
//Display the image on the page
echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" />';
?> 

 

One way to do it.

how it all work am i nearly there taking a blind shot at this posted code.

 

glob("/path/to/images/*.{gif,jpg}", GLOB_BRACE);

 

glob >>> get all the image names via a path.

 

("/path/to/images/ <<< path

 

*.{gif,jpg}", // wildcard * letrail . { open brace gif , <<< and jpg images } << close brace

 

, <<< another exspesion comming

 

GLOB_BRACE  <<<< look for 1 extention or another ); <<< close

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.