有没有一种简单的方法来读取目录中的文件名,并添加到一个数组? [英] Is there an easy way to read filenames in a directory and add to an array?

查看:158
本文介绍了有没有一种简单的方法来读取目录中的文件名,并添加到一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录:音频/ 并在这将是唯一的MP3文件。我想自动创建链接到这些文件的过程。有没有一种方法来读取一个目录,该目录中的文件名添加到一个数组中?

I have a directory: Audio/ and in that will be mp3 files only. I'm wanting to automate the process of creating links to those files. Is there a way to read a directory and add filenames within that directory to an array?

这将会是倍加冷静,如果我们可以做一个关联数组,并具有关键是文件名减去.MP3标签。

It'd be doubly cool if we could do an associative array, and have the key be the file name minus the .mp3 tag.

任何想法?

要详细说明:我实际有几个音频/ 文件夹和每个文件夹包含不同事件的MP3。事件细节正在从数据库和填充表拉升。这就是为什么我在重复code,因为现在每个音频/ 文件夹中,我有定义下载链接的文件名和定义文件名MP3播放器。

To elaborate: I actual have several Audio/ folders and each folder contains mp3s of a different event. The event details are being pulled from a database and populating a table. That's why I'm duplicating code, because right now in each Audio/ folder, I'm having to define the filenames for the download links and define the filenames for the mp3 player.

感谢您!这将大大简化我的code作为现在我重复吨code遍地!

Thank you! This will greatly simplify my code as right now I'm repeating tons of code over and over!

推荐答案

的SPL方法是使用的 DirectoryIterator

The SPL way is with DirectoryIterator:

$files = array();
foreach (new DirectoryIterator('/path/to/files/') as $fileInfo) {
    if($fileInfo->isDot() || !$fileInfo->isFile()) continue;
    $files[] = $fileInfo->getFilename();
}

这篇关于有没有一种简单的方法来读取目录中的文件名,并添加到一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆