Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.