How do I get a random image from a directory?

Use the RandRange function to pick a random image file form your directory.

<cfset imagepath = "images/gallery/" />
<cfdirectory directory="#ExpandPath(imagepath)#"
 filter="*.jpg|*.gif"
 name="qryImages"
 action="list" />
<img src="#imagepath##qryImages.name[RandRange(1, qryImages.RecordCount)]#" />

This question was written by John Whish
It was last updated on July 10, 2008.

Categories

Images

Comments

comments powered by Disqus