Posted By

klovera on 10/18/11


Tagged


Versions (?)

function: getImages($folder)


 / Published in: PHP
 

Returns all the images in a folder randomly.Listed back in this format: { src: "/'.$localdir.$file.'" },

  1. function getImages($folder){
  2.  
  3. $basedir = "/home/cochipp/public_html/"; // base directory for the server
  4.  
  5. $localdir = "images/banners/".$folder."/"; // local directory and the folder passed
  6. $dirname = $basedir.$localdir;
  7.  
  8. $dir = opendir($dirname);
  9.  
  10. $images_arr = array();
  11. $images = "";
  12. $i = 0;
  13.  
  14. while(false != ($file = readdir($dir))){
  15. if(($file != ".") and ($file != "..") and ($file != "index.html")){
  16. $images_arr[$i] = '{ src: "/'.$localdir.$file.'" },';
  17. $i++;
  18. }
  19. }
  20.  
  21. shuffle($images_arr);
  22.  
  23. foreach($images_arr as $image){
  24. $images .= $image;
  25. }
  26.  
  27. // strip off last comma
  28. $images = substr($images, 0, -1);
  29.  
  30. echo $images;
  31.  
  32. }

Report this snippet  

You need to login to post a comment.