开发者

PHP SPL RecursiveDirectoryIterator - getPath and ltrim path

开发者 https://www.devze.com 2023-04-12 05:09 出处:网络
n00b here, be patient:) I need to get a list of jpgs from my images directory and have it\'s subdirectory names appear as the CSS div class for a given image. I can get this to work, b开发者_如何学JA

n00b here, be patient:)

I need to get a list of jpgs from my images directory and have it's subdirectory names appear as the CSS div class for a given image. I can get this to work, b开发者_如何学JAVAut I can't figure out how to get just the enclosing directory name as the div class without any of the path leading up to it. i.e

path to image is: images2/food/hotdog.jpg I need: <div class="food"><a href="images2/food/hotdog.jpg">

The below works but doesn't create the array, I only get one image. If I remove my $path and $folder attempt, and have $thelist .= 'getPath().' it works but I get <div class="images2/food"> and my javascript doesn't like that.

Here is my code:

<?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?>


You just seem to be missing some curly brackets. You should put everything you want to happen in the foreach loop inside curly brackets. The same thing with the if statement. Without the curly brackets, only the next line applies to that previous statement.

Also, I think your parameter for your ltrim statement should be "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?>


AndrewR already presented a solution you can use. It doesn't handle two edge-cases though. What if your file's extension is "JPG"? What if your file doesn't have an extension at all?

<?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>';
}


Protip : file extensions are as easy as :

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

So lets :

$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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号