当前位置:首页 > PHP教程 > php图像处理 > 列表

php 读取目录下图像文件

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-19 15:05:33 浏览: 评论:0 

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

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

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

Tags: php读取目录 php读取图像文件

分享到: