PHP SPL RecursiveDirectoryIterator - getPath 和 ltrim 路径
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎缺少一些大括号。您应该将您想要在 foreach 循环中发生的所有内容放在大括号内。 if 语句也是如此。如果没有大括号,则只有下一行适用于上一条语句。
另外,我认为 ltrim 语句的参数应该是“images2/”。
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/".
AndrewR 已经提出了一个您可以使用的解决方案。但它不能处理两种边缘情况。如果您的文件扩展名是“JPG”怎么办?如果您的文件根本没有扩展名怎么办?
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?
专业提示:文件扩展名就像这样简单:
end(explode(".", $path));
所以让我们:
Protip : file extensions are as easy as :
end(explode(".", $path));
So lets :