首页 > 语言 > PHP > 正文

php 读取目录下图像文件

2024-09-04 11:44:19
字体:大 中 小
来源:转载
供稿:网友

本款是一款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. //开源代码Vevb.com 
  36. closedir($dir_handle); 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表