四海人民公摄 - 海外华人摄影爱好者论坛

 找回密码
 注册
搜索
热搜: 活动 交友 discuz
查看: 1144|回复: 0

php random images from a folder

[复制链接]
发表于 2009-12-18 14:55:17 | 显示全部楼层 |阅读模式
Ever wanted to display display a random avatar or banners on your forum or website? Continue reading to find out how...

The Script

Here's the script. Call it something like images.php:

  1. <?php
  2. Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
  3. Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
  4. Header("Pragma: no-cache");
  5. Header("Content-Type: image/gif");

  6. $dir = "Images"; // This is the folder where the images are

  7. srand((double)microtime()*1000000);
  8. $i = 0;
  9. $dirHandle = opendir($dir); // Open the images folder
  10. while(($im = readdir($dirHandle)))
  11. {
  12. if($im != ".." && $im != ".") // Don't read in the 2 folders ".." and "."
  13. {
  14. $image[$i] = $im; // Select an image
  15. $i++;
  16. }
  17. }
  18. closedir($dirHandle); // Close the folder
  19. $n = rand(0,(count($image)-1));

  20. if(!readfile($dir."/".$image[$n])) // Read the image
  21. readfile($dir."error/error.gif"); // If the script can't find the directory, display this image
  22. ?>
复制代码




And now on your page, put this:

<img src="http://www.yourdomain.com/images.php" border="0">

And there you have it! It will randomly display any image in the images folder!

Enjoy!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|四海人民公摄 - 海外华人摄影爱好者网站

GMT+8, 2024-5-3 14:47

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表