paparanch Posted March 10, 2009 Share Posted March 10, 2009 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].'" />'; ?> Link to comment https://forums.phpfreaks.com/topic/148713-solved-display-both-jpg-and-gif-file-from-directorypls-help/ Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 <?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. Link to comment https://forums.phpfreaks.com/topic/148713-solved-display-both-jpg-and-gif-file-from-directorypls-help/#findComment-780866 Share on other sites More sharing options...
paparanch Posted March 10, 2009 Author Share Posted March 10, 2009 wew! but i only have to get 1 image from that folder...its either gif or jpg... Link to comment https://forums.phpfreaks.com/topic/148713-solved-display-both-jpg-and-gif-file-from-directorypls-help/#findComment-780871 Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 foreach You need to loop through the results. Link to comment https://forums.phpfreaks.com/topic/148713-solved-display-both-jpg-and-gif-file-from-directorypls-help/#findComment-780879 Share on other sites More sharing options...
.josh Posted March 10, 2009 Share Posted March 10, 2009 $img_array = glob("/path/to/images/*.{gif,jpg}", GLOB_BRACE); Link to comment https://forums.phpfreaks.com/topic/148713-solved-display-both-jpg-and-gif-file-from-directorypls-help/#findComment-780884 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 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 Link to comment https://forums.phpfreaks.com/topic/148713-solved-display-both-jpg-and-gif-file-from-directorypls-help/#findComment-780889 Share on other sites More sharing options...
paparanch Posted March 10, 2009 Author Share Posted March 10, 2009 wow! problem solved masters! tnx for the help! more power phpfreaks! Link to comment https://forums.phpfreaks.com/topic/148713-solved-display-both-jpg-and-gif-file-from-directorypls-help/#findComment-780950 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.