2013-10-19 108 views
1

如何显示子文件夹中的所有图像,如下所示:$ dir = array('images/posters','images/designs','images/illustrations'); ??php在文件夹和子文件夹中显示图像

代码:

<?php 
    $dir = 'img/werk/'; 
    $file_display = array('jpg', 'jpeg', 'png', 'gif'); 

    if (file_exists($dir) == false) { 
     echo 'Directory \'', $dir, '\' not found!'; 
    } else { 
    $dir_content = scandir($dir); 

    foreach ($dir_content as $file) { 
     $file_type = strtolower(end(explode('.', $file))); 

     if ($file !== '.' && $file !== '..' 
          && in_array($file_type, $file_display) == true) { 
      echo '<div class="thing large"><div class="item_description">Item description</div><img src="', $dir, '/', $file, '" /></div>'; 
     } 
     } 
    } 
?> 

回答

1

这是我采取了一个文件夹,在其中显示的所有图像,并显示它在网页上。 (全部在php中)

该代码将图像(来自不同格式)并将它们放在网页上,并在文件名下面。

$files = glob($root."Images/*.*"); 
echo '<table width="750px" id="autoimage" align="center">'; 
for ($i=0; $i<count($files); $i++) 
{ 
echo '<tr height="20px"><td colspan="2"></td></tr><tr><td width="325px" align="center">'; 
if(isset($files[$i])){ 
    $num = $files[$i]; 
    echo '<img width="300px" src="'.$num.'" alt="Image" />'."<br /><br />"; 
    $num1 = str_replace($root.'Images/', '', $num); 
    if (strpos($num1,'.png') == true) 
    { 
     $text = str_replace('.png', '', $num1); 
    } 
    if (strpos($num1,'.jpeg') == true) 
    { 
     $text = str_replace('.jpeg', '', $num1); 
    } 
    if (strpos($num1,'.jpg') == true) 
    { 
     $text = str_replace('.jpg', '', $num1); 
    } 
    if (strpos($num1,'.tiff') == true) 
    { 
     $text = str_replace('.tiff', '', $num1); 
    } 
    if (strpos($num1,'.gif') == true) 
    { 
     $text = str_replace('.gif', '', $num1); 
    } 
    print "<p>".$text."</p><br />"; 
} 
echo '</td></tr><tr height="20px"><td colspan="2"></td></tr><tr>'; 
} 
echo '</table>'; 

要获取子文件夹,请执行类似的操作,但更改代码所在的位置。

+0

非常感谢,这是很好的代码,但我已经解决了它与解决方法:)爱这个论坛 – Auguste

相关问题