php 读取目录下图像文件

本款是一款php 读取目录下图像文件代码,利用了opendir来打开目录然后获取文件后缀名,判断是否为指定文件.

php 读取目录下图像文件实例代码如下:

  1. $directory = 'gallery';
  2. $allowed_types=array('jpg','jpeg','gif','png');
  3. $file_parts=array();
  4. $ext='';
  5. $title='';
  6. $i=0;
  7. $dir_handle = @opendir($directory) or die("there is an error with your image directory!");
  8. while ($file = readdir($dir_handle))
  9. {
  10. if($file=='.' || $file == '..') continue;
  11. $file_parts = explode('.',$file);
  12. $ext = strtolower(array_pop($file_parts));
  13. $title = implode('.',$file_parts);
  14. $title = htmlspecialchars($title);
  15. $nomargin='';
  16. if(in_array($ext,$allowed_types))
  17. {
  18. if(($i+1)%4==0) $nomargin='nomargin';
  19. echo '
  20. <div class="pic '.$nomargin.'" .$directory.'/'.$file.') no-repeat 50% 50%;">
  21. <a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
  22. </div>';
  23. $i++;
  24. }
  25. }
  26. //开源代码phpfensi.com
  27. closedir($dir_handle);