1

n00b在这里,请耐心等待:)

我需要从我的图像目录中获取 jpg 列表,并将其子目录名称显示为给定图像的 CSS div 类。我可以让它工作,但我不知道如何在没有任何路径的情况下将封闭目录名称作为 div 类。IE

图像路径是:images2/food/hotdog.jpg 我需要: <div class="food"><a href="images2/food/hotdog.jpg">

以下工作但不创建数组,我只得到一张图像。如果我删除了我的 $path 和 $folder 尝试,并且有 $thelist .= 'getPath().' 它有效,但我明白<div class="images2/food">了,我的 javascript 不喜欢那样。

这是我的代码:

<?php
$it = new RecursiveDirectoryIterator('images2');
$display = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)

if ( In_Array ( SubStr ( $file, StrrPos ( $file, '.' ) + 1 ), $display ) == true )

$path = $file->getPath();
$folder = ltrim($path, "/images2");

$thelist .= '<div class="'.$folder.'"><a href="'.$file->getPath().'/'.$file->getFilename().'"rel="shadowbox['.$file->getPath().']">'.'<img src="../slir/w180-h180-c1:1/test/isotope/'.$file->getPath().'/'.$file->getFilename().'" /></a></div>'; 

?>    

<?=$thelist?>
4

3 回答 3

1

您似乎只是缺少一些大括号。你应该把你想要发生的一切都放在大括号内的 foreach 循环中。if 语句也是如此。没有大括号,只有下一行适用于前面的语句。

另外,我认为您的 ltrim 语句的参数应该是“images2/”。

<?php
$it = new RecursiveDirectoryIterator('images2');
$display = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)
{
    if ( In_Array ( SubStr ( $file, StrrPos ( $file, '.' ) + 1 ), $display ) == true )
    {
        $path = $file->getPath();
        $folder = ltrim($path, "images2/");

        $thelist .= '<div class="'.$folder.'"><a href="'.$file->getPath().'/'.$file->getFilename().'"rel="shadowbox['.$file->getPath().']">'.'<img src="../slir/w180-h180-c1:1/test/isotope/'.$file->getPath().'/'.$file->getFilename().'" /></a></div>'; 
    }
}

?>    

<?=$thelist?>
于 2011-10-10T17:33:20.110 回答
1

AndrewR 已经提出了一个您可以使用的解决方案。但它不处理两个边缘情况。如果您的文件扩展名为“JPG”怎么办?如果您的文件根本没有扩展名怎么办?

<?php
$theList = ''; // initialize the variables you're using!
$allowed = array("jpg" => true, "jpeg" => true);
$it = new RecursiveDirectoryIterator('images2');
$rit = new RecursiveIteratorIterator($it);
foreach ($rit as $file) {
  $pos = strrpos($file, '.');
  if ($pos === false) {
    // file doesn't contain a . (has no extension)
    continue;
  }
  // file might be named foo.JPG (sadly quite common for windows / certain cameras)
  $extension = strtolower(substr($file, $pos+1));
  if (!isset($allowed[$extension])) { // this is a bit faster than in_array()
    // $extension is not allowed
    continue;
  }

  // what happens with your class, when you encounter a sub-directory like images2/my-images/easter-holidays/foo.jpg
  // that won't be accessible from CSS (without major escaping action)
  // try to keep classNames alphanumeric (like-this-class-name)
  $path = $file->getPath();
  $folder = ltrim($path, "images2/");

  // always, ALWAYS, respect the context! 
  // htmlspecialchars() go around any non-literal output you make!
  $thelist .= '<div class="' . htmlspecialchars($folder) . '"><a href="'
    . htmlspecialchars($file->getPath()) . '/' . htmlspecialchars($file->getFilename()) 
    . '"rel="shadowbox[' . htmlspecialchars($file->getPath()) . ']">' 
    . '<img src="../slir/w180-h180-c1:1/test/isotope/' . htmlspecialchars($file->getPath()) 
    . '/' . htmlspecialchars($file->getFilename()) . '" /></a></div>';
}
于 2011-10-10T17:54:38.380 回答
1

Protip:文件扩展名很简单:

end(explode(".", $path));

让我们 :

$ite = new RecursiveDirectoryIterator("/mnt/shared/Media/Music/");
// Here are some extensions I want to keep
$keepers = array('ogg', 'mp3', 'wav', 'flac');
foreach (new RecursiveIteratorIterator($ite) as $filename => $cur) {
    // This bit of magic lowercases the file extension by grabbing everything past the last
    // '.' and checking whether said string is within the $keepers array, otherwise we just
    // skip this iteration and go on to the next.
    if (!in_array(end(explode(".", strtolower($filename))), $keepers)) { continue; }
    // Every $filename past here is fine as wine
    doit($filename); // :D
}
于 2013-01-30T10:32:10.767 回答