PHP SPL RecursiveDirectoryIterator - getPath 和 ltrim 路径

发布于 2024-12-09 05:31:59 字数 1098 浏览 2 评论 0原文

n00b 这里,请耐心等待:)

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

图像路径是:images2/food/hotdog.jpg 我需要:

下面的代码可以工作,但不会创建数组,我只得到一张图像。如果我删除 $path 和 $folder 尝试,并且有 $thelist .= 'getPath().'它可以工作,但我得到

而我的 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?>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染年凉城似染瑾 2024-12-16 05:31:59

您似乎缺少一些大括号。您应该将您想要在 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?>

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?>
我的影子我的梦 2024-12-16 05:31:59

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

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>';
}
别想她 2024-12-16 05:31:59

专业提示:文件扩展名就像这样简单:

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文