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

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

问题描述

我有一个目录: Audio / ,其中只有mp3文件。我想要自动化创建到这些文件的链接的过程。有没有办法读取目录并在该目录中添加文件名到数组?



如果我们可以做一个关联数组,并且将该文件名称减去.mp3标签,那么这个双引号就会很酷。



任何想法?



详细说明:我实际有几个音频/ 文件夹和每个文件夹包含不同事件的mp3。事件详细信息从数据库中拉出并填充表。这就是为什么我复制代码,因为现在在每个 Audio / 文件夹中,我必须定义下载链接的文件名,并定义mp3播放器的文件名。



谢谢!这将大大简化我的代码,现在我一遍又一遍地重复了大量的代码!

解决方案

SPL方式是 DirectoryIterator



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


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?

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

Any ideas?

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.

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

解决方案

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天全站免登陆