Home  >  Article  >  Backend Development  >  php function to randomly display images

php function to randomly display images

WBOY
WBOYOriginal
2016-07-25 08:56:061064browse
  1. /**
  2. * Function: Randomly display pictures
  3. * Filename: img.php
  4. * Usage:
  5. *
  6. *
  7. * Edit: bbs.it-home.org
  8. **/
  9. if($_GET['folder']){
  10. $folder=$_GET['folder'];
  11. }else{
  12. $folder='/images/';
  13. }
  14. //存放图片文件的位置
  15. $path = $_SERVER['DOCUMENT_ROOT']."/".$folder;
  16. $files=array();
  17. if ($handle=opendir("$path")) {
  18. while(false !== ($file = readdir($handle))) {
  19. if ($file != "." && $file != "..") {
  20. if(substr($file,-3)=='gif' || substr($file,-3)=='jpg') $files[count($files)] = $file;
  21. }
  22. }
  23. }
  24. closedir($handle);
  25. $random=rand(0,count($files)-1);
  26. if(substr($files[$random],-3)=='gif') header("Content-type: image/gif");
  27. elseif(substr($files[$random],-3)=='jpg') header("Content-type: image/jpeg");
  28. readfile("$path/$files[$random]");
  29. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn