Preloader Script

This is a simple image preloading script I created in PHP for my own site. It basically reads the media file types of your choice, from a folder that you specify into an array.

The benefit this small script has for me is that I can add or remove images from my media folder and not have to update my HTML code...!

Source Code

  1. <?php
  2. function loadMedia($directory)
  3. {
  4. $thumbnail = '';
  5. $thumbnails = array();
  6.  
  7. if($handle = dir($directory))
  8. {
  9. while(false !== ($thumbnail = $handle->read()))
  10. {
  11. if(preg_match('/.(png|gif|jpe?g)$/i',$thumbnail))
  12. {
  13. $thumbnails[] = "'/$directory$thumbnail'";
  14. }
  15. }
  16.  
  17. $handle->close();
  18. }
  19.  
  20. unset($handle,$directory,$thumbnail);
  21.  
  22. return $thumbnails;
  23. }
  24.  
  25. $preloaded = $thumbnail = '';
  26. $thumbnails = array();
  27. $thumbnails = loadMedia('media/');
  28.  
  29. foreach($thumbnails as $image)
  30. {
  31. $thumbnail .= $image.',';
  32. }
  33.  
  34. $preloaded = substr($thumbnail,0,-1);
  35. ?>
  36. <body onload="javascript:MM_preloadImages(<?php echo $preloaded; ?>);">

Share These Free Scripts

My Fancy Artistic Separator

Categories