PHP SPL RecursiveDirectoryIterator - getPath和ltrim路径

时间:2011-10-10 17:17:21

标签: php iterator directory trim spl

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

我需要从我的images目录中获取一个jpgs列表,并让它的子目录名称显示为给定图像的CSS div类。我可以让这个工作,但我无法弄清楚如何获得封闭的目录名称作为div类没有任何导致它的路径。即

图像的路径是: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?>

3 个答案:

答案 0 :(得分: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?>

答案 1 :(得分: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>';
}

答案 2 :(得分: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
}