显示目录中所有文件的问题
嘿伙计们,我正在寻找一种方法来显示目录中的所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该代码根本不起作用。当您将 $file 变量设置为“..”时,结果将是大量包含 $dirPath 和“..”的 xml。
这就是您正在寻找的:)
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 :)
您必须创建一个递归函数来搜索所有 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 ofif ($file = ".mp3" && $file = "..") {
, and after that's changed, you get a condition that's always false. What are you trying to do there?就像 icktoofay 所说,你必须创建一个递归函数。另外,您的代码有一个错误:
无法工作(并且
if ($file == ".mp3" && $file == "..") {
也是错误的) 。该行应该如下所示:如果你想显示“..” - 否则它就像这样:
like icktoofay said, you'll have to make a recursive function. also, your code has an error:
won't work (and
if ($file == ".mp3" && $file == "..") {
is wrong, too). that line should look like this:if you want to show the ".." - else it's just like this: