【问题标题】:php loop through files in subfolders in folder and get file pathsphp遍历文件夹中子文件夹中的文件并获取文件路径
【发布时间】:2016-06-03 05:06:58
【问题描述】:

我真的是 PHP 新手,我搜索了 google 以找到一个正确的脚本,该脚本循环遍历文件夹中的所有子文件夹并获取该子文件夹中的所有文件路径

<?php
    $di = new RecursiveDirectoryIterator('posts');
    foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
    echo $filename. '<br/>';
    }
?>

所以我有文件夹“posts”,其中有子文件夹“post001”,其中有两个文件

controls.png
text.txt

上面的代码也反映了这一点

posts\.
posts\..
posts\post001\.
posts\post001\..
posts\post001\controls.png
posts\post001\text.txt

但我只想像这样回显这些子文件夹中的文件路径

posts\post001\controls.png
posts\post001\text.txt

这一切的重点是我想为每个子文件夹动态创建 div,在这个 div 里面我把 img 和 src 和一些 h3 和 p html 标签的文本等于 .txt 文件所以这是正确的做法那以及如何重新制作我的 php 脚本,以便我只获得文件路径

所以我可以看到答案,它们都是正确的,但现在我的意思是我需要这样的东西

foreach( glob( 'posts/*/*' ) as $filePath ){
 //create div with javascript
  foreach( glob( 'posts/$filePath/*' ) as $file ){
      //append img and h3 and p html tags to the div via javascript
  }
  //append the created div somewhere in the html again via javascript
}

那么在 php 中执行这两个 foreach 循环的正确语法是什么,我现在真的掌握了基础知识

【问题讨论】:

  • 这可能是一个糟糕的建议,但您可以在 echo 之前添加一个 if(strpos($filename, '\.') === false)
  • 你所说的糟糕建议是指你的建议或我对完成任务的想法
  • 我的意思是可能有更好的方法来做你想做的事,而不会浪费 CPU 检查每个结果。我以前从未见过您在那里使用的功能,这只是解决您问题的一种黑客方式。
  • 我不知道它们是否是某些 Windows 限制,但在 Unix 上,这个独特的命令可以按您的预期工作:foreach( glob( 'posts/*/*' ) as $filePath )
  • 不错的 fusion3k 将其作为答案,所以我可以接受答案

标签: php html


【解决方案1】:

看看这个:

<?php
$filename[] = 'posts\.';
$filename[] = 'posts\..';
$filename[] = 'posts\post001\.';
$filename[] = 'posts\post001\..';
$filename[] = 'posts\post001\controls.png';
$filename[] = 'posts\post001\text.txt';

foreach ($filename as $file) {
    if (substr($file, -4, 1) === ".") {
        echo $file."<br>";
    }
}
?>

结果:

posts\post001\controls.png
posts\post001\text.txt

它的作用是检查倒数第四位是否为点。如果是这样,它是三个字母的扩展名,它应该是一个文件。您还可以检查特定的扩展名。

$ext = substr($file, -4, 4);
if ($ext === ".gif" || $ext === ".jpg" || $ext === ".png" || $ext === ".txt") {
    echo $file."<br>";
}

【讨论】:

  • 没有扩展名的文件怎么办?还有扩展名短于或长于 3 的文件? (.py, .c, .conf)
  • 为什么投反对票?他在谈论 .txt 和图像文件。所以主要是三个字母的扩展......
  • 在回答时,您必须考虑其他人可能会偶然发现这个问题并尝试使用代码。他举了两个文件的例子,从来没有说过它只会是图像和文本。但我投了反对票,原因与我评论我的代码而不是将其作为答案发布的原因相同:它很老套,效率低下,仅适用于某些场景并且可能会产生意想不到的结果。
  • 我引用:“img with src and some h3 and p html tags with text equal to the .txt file”
【解决方案2】:
<h1>Directory Listing</h1>
<?php
/**
 * Recursive function to append the full path of all files in a
 * given directory $dirpath to an array $context
 */
function getFilelist($dirpath, &$context){
  $fileArray = scandir($dirpath);
  if (count($fileArray) > 2) {
    /* Remove the . (current directory) and .. (parent directory) */
    array_shift($fileArray);
    array_shift($fileArray);
    foreach ($fileArray as $f) {
      $full_path = $dirpath . DIRECTORY_SEPARATOR . $f;
      /* If the file is a directory, call the function recursively */
      if (is_dir($full_path)) {
        getFilelist($full_path, $context);
      } else {
        /* else, append the full path of the file to the context array */
        $context[] = $full_path;
      }
    }
  }
}
/* $d is the root directory that you want to list */
$d = '/Users/Shared';
/* Allocate the array to store of file paths of all children */
$result = array();
getFilelist($d, $result);
$display_length = false;
if ($display_length) {
    echo 'length = ' . count($result) . '<br>';
}
function FormatArrayAsUnorderedList($context) {
    $ul = '<ul>';
    foreach ($context as $c) {
      $ul .= '<li>' . $c . '</li>';
    }
    $ul .= '</ul>';
    return $ul;
}
$html_list = FormatArrayAsUnorderedList($result);
echo $html_list;
?>

【讨论】:

    【解决方案3】:

    看看这是否有效:)

    $di = new RecursiveDirectoryIterator('posts');
    foreach (new RecursiveIteratorIterator($di) as $filename => $file) {           
        if ((substr($file, -1) != '.') && (substr($file, -2) != '..')) {
            echo $file . '<br/>';
        }
    }
    

    【讨论】:

    • 它可以工作,但是我考虑了两个 foreach 循环,以便在第二个循环之前我可以创建 div(每个子文件夹的 div),然后在第二个循环内添加 html 元素 insive 这个 div 所以这个递归函数不是我的想法
    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 2014-09-28
    • 2010-12-02
    • 2013-11-24
    • 2023-01-20
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多