显示目录中所有文件的问题

发布于 2024-09-07 23:24:19 字数 576 浏览 2 评论 0原文

嘿伙计们,我正在寻找一种方法来显示目录中的所有 mp3 文件,

这是我的代码:

    if ($handle = opendir($dirPath)) {

       while (false !== ($file = readdir($handle))) {

         if ($file = ".mp3" && $file = "..") {
             echo '
             <track>
              <location>'.$dirPath.$file.'</location>
              <creator>'.$file.'</creator>
            </track>
            ';    

          }
       }
   closedir($handle);
}

现在我知道这个脚本只会显示父目录中的 mp3 文件,但我需要显示所有目录中的所有 mp3 文件父目录

问题是此代码无法显示子目录内的文件!

hey guys im looking for a way to show all mp3 files in a directory

this is my code to get that :

    if ($handle = opendir($dirPath)) {

       while (false !== ($file = readdir($handle))) {

         if ($file = ".mp3" && $file = "..") {
             echo '
             <track>
              <location>'.$dirPath.$file.'</location>
              <creator>'.$file.'</creator>
            </track>
            ';    

          }
       }
   closedir($handle);
}

now i know that this script will only show mp3 files in parent directory , but i need to show all mp3 files in all directory inside parent directory

problem is this code cant show files inside sub directories !

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

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

发布评论

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

评论(3

信愁 2024-09-14 23:24:19

该代码根本不起作用。当您将 $file 变量设置为“..”时,结果将是大量包含 $dirPath 和“..”的 xml。

这就是您正在寻找的:)

$it = new RecursiveDirectoryIterator('path/to/files/');
foreach (new RecursiveIteratorIterator($it) as $file)
{
    echo $file->getPathname() . '<br />';
}

That code won't work at all. As you are setting the $file variable to ".." the result will be a lot of xml containing $dirPath and "..".

This is what you are looking for :)

$it = new RecursiveDirectoryIterator('path/to/files/');
foreach (new RecursiveIteratorIterator($it) as $file)
{
    echo $file->getPathname() . '<br />';
}
柒七 2024-09-14 23:24:19

您必须创建一个递归函数来搜索所有 MP3。

另外,您可能的意思是 if ($file == ".mp3" && $file == "..") { 而不是 if ($file = ".mp3" && $file = "..") {,更改后,您得到的条件始终为 false。你想在那里做什么?

You'll have to make a recursive function to search for all the MP3s.

Also, you probably meant if ($file == ".mp3" && $file == "..") { instead of if ($file = ".mp3" && $file = "..") {, and after that's changed, you get a condition that's always false. What are you trying to do there?

誰認得朕 2024-09-14 23:24:19

就像 icktoofay 所说,你必须创建一个递归函数。另外,您的代码有一个错误:

if ($file = ".mp3" && $file = "..") {

无法工作(并且 if ($file == ".mp3" && $file == "..") { 也是错误的) 。该行应该如下所示:

if (substr($file,-4) == ".mp3" || $file == "..") {

如果你想显示“..” - 否则它就像这样:

if (substr($file,-4) == ".mp3") {

like icktoofay said, you'll have to make a recursive function. also, your code has an error:

if ($file = ".mp3" && $file = "..") {

won't work (and if ($file == ".mp3" && $file == "..") { is wrong, too). that line should look like this:

if (substr($file,-4) == ".mp3" || $file == "..") {

if you want to show the ".." - else it's just like this:

if (substr($file,-4) == ".mp3") {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文