Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP SPL RecursiveDirectoryIterator-getPath和ltrim路径_Php_Iterator_Directory_Trim_Spl - Fatal编程技术网

PHP SPL RecursiveDirectoryIterator-getPath和ltrim路径

PHP SPL RecursiveDirectoryIterator-getPath和ltrim路径,php,iterator,directory,trim,spl,Php,Iterator,Directory,Trim,Spl,这里是n00b,耐心点:) 我需要从images目录中获取一个JPG列表,并将其子目录名称显示为给定图像的CSS div类。我可以做到这一点,但我不知道如何在没有任何指向它的路径的情况下,只获取作为div类的封闭目录名。i、 e 图像路径为:images2/food/hotdog.jpg 我需要: ”; ?> 您似乎缺少了一些花括号。你应该把所有你想发生的事情放在foreach循环的花括号里。if语句也是如此。如果没有花括号,则只有下一行应用于上一条语句 另外,我认为ltrim语句的参数

这里是n00b,耐心点:)

我需要从images目录中获取一个JPG列表,并将其子目录名称显示为给定图像的CSS div类。我可以做到这一点,但我不知道如何在没有任何指向它的路径的情况下,只获取作为div类的封闭目录名。i、 e

图像路径为:images2/food/hotdog.jpg 我需要:
”;
?>    

您似乎缺少了一些花括号。你应该把所有你想发生的事情放在foreach循环的花括号里。if语句也是如此。如果没有花括号,则只有下一行应用于上一条语句

另外,我认为ltrim语句的参数应该是“images2/”


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

Protip:文件扩展名与以下内容一样简单:

end(分解(“.”,$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
}

我想这可能是语法问题(卷曲的引号)。你的解释帮助很大。非常感谢。这看起来很棒,非常完整。我必须查找htmlspecialcharacters函数,但这很有意义。不幸的是,当我尝试这一个,它没有工作。只是一张空白页。
<?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>';
}
$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
}