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

发布于 2024-09-15 13:07:45 字数 356 浏览 2 评论 0原文

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

如果我们可以做一个关联数组,并且键是文件名减去 .mp3 标签,那就加倍酷了。

有什么想法吗?

详细说明:我实际上有几个 Audio/ 文件夹,每个文件夹都包含不同事件的 mp3。正在从数据库中提取事件详细信息并填充表。这就是我复制代码的原因,因为现在在每个 Audio/ 文件夹中,我必须定义下载链接的文件名并定义 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?

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!

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

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

发布评论

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

评论(6

鸠魁 2024-09-22 13:07:45

SPL 方式是使用 DirectoryIterator

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

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();
}
后来的我们 2024-09-22 13:07:45

为了完整起见:您也可以使用 glob

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

这将返回所有文件(但不是文件夹) ,您可以根据需要进行调整。

要仅获取文件名(而不是具有完整路径的文件),只需添加:

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

And for completeness : you could use glob as well :

$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);
未蓝澄海的烟 2024-09-22 13:07:45

是:使用 scandir()。如果您只想要不带扩展名的文件名,请使用 basename () 位于从 scandir() 接收到的数组中的每个元素上。

Yes: use 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().

暗喜 2024-09-22 13:07:45

这应该能够满足您的要求:

// 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);

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);
贱贱哒 2024-09-22 13:07:45

当然...我认为这应该有效...

$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);

我想这应该有效...

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...

天气好吗我好吗 2024-09-22 13:07:45
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
  if ($file != "." && $file != "..") {
    $results[] = $file;
  }
}
closedir($handler);

这应该可行,如果您希望从数组中排除任何文件,只需将它们添加到 if 语句中,文件扩展名相同

$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

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