1

ここでn00b、我慢してください:)

画像ディレクトリから jpg のリストを取得し、そのサブディレクトリ名を特定の画像の CSS div クラスとして表示する必要があります。これを機能させることはできますが、そこに至るまでのパスなしで、囲んでいるディレクトリ名だけを div クラスとして取得する方法がわかりません。すなわち

画像へのパスは次のとおりです: images2/food/hotdog.jpg <div class="food"><a href="images2/food/hotdog.jpg">

以下は機能しますが、配列を作成しません。画像は 1 つしか取得できません。$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 は、使用できるソリューションを既に提示しています。ただし、2 つのエッジケースは処理しません。ファイルの拡張子が「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 に答える