简体   繁体   English

有没有一种简单的方法来读取目录中的文件名并添加到数组?

[英]Is there an easy way to read filenames in a directory and add to an array?

I have a directory: Audio/ and in that will be mp3 files only. 我有一个目录: Audio/其中只有mp3文件。 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. 如果我们可以做一个关联数组,并且密钥是文件名减去.mp3标签,那将会非常酷。

Any ideas? 有任何想法吗?

To elaborate: I actual have several Audio/ folders and each folder contains mp3s of a different event. 详细说明:我实际上有几个Audio/文件夹,每个文件夹包含不同事件的mp3。 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. 这就是我复制代码的原因,因为现在每个Audio/文件夹中,我必须为下载链接定义文件名并定义mp3播放器的文件名。

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 : SPL方式是使用DirectoryIterator

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

And for completeness : you could use glob as well : 为了完整性:你也可以使用glob

$files = array_filter(glob('/path/to/files/*'), 'is_file'); 

This will return all files (but not the folders), you can adapt it as needed. 这将返回所有文件(但不是文件夹),您可以根据需要进行调整。

To get just the filenames (instead of files with complete path), just add : 要获取文件名(而不是具有完整路径的文件),只需添加:

$files = array_map('basename', $files);

Yes: use scandir() . 是的:使用scandir() If you just want the name of the file without the extension, use basename() on each element in the array you received from scandir() . 如果您只想要没有扩展名的文件名,请在从scandir()收到的数组中的每个元素上使用basename() scandir()

This should be able to do what you're looking for: 这应该能够做你想要的:

// Read files
$files = scandir($dirName);
// Filter out non-files ('.' or '..')
$files = array_filter($files, 'is_file');
// Create associative array ('filename' => 'filename.mp3')
$files = array_combine(array_map('basename', $files), $files);

Sure...I think this should work... 当然......我认为这应该有效......

$files[] = array();
$dir = opendir("/path/to/Audio") or die("Unable to open folder");
    while ($file = readdir($dir)) {
        $cleanfile = basename($file);
        $files[$cleanfile] = $file;
    }
    closedir($dir);

I imagine that should work... 我想这应该工作......

$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
  if ($file != "." && $file != "..") {
    $results[] = $file;
  }
}
closedir($handler);

this should work, if you want any files to be excluded from the array, just add them to the if statement, same for file extensions 这应该工作,如果你想从数组中排除任何文件,只需将它们添加到if语句,对于文件扩展名也是如此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM