PHP:如何在不扫描整个目录的情况下从目录中获取单个文件?

标签 php file directory

我有一个包含 130 万个文件的目录,我需要将这些文件移入数据库。我只需要从目录中获取一个文件名而不扫描整个目录。我抓取哪个文件并不重要,因为我会在完成后删除它,然后继续下一个。这可能吗?我能找到的所有示例似乎都将整个目录列表扫描到一个数组中。我只需要一次抓取一个进行处理……而不是每次 130 万。

最佳答案

应该这样做:

<?php
$h = opendir('./'); //Open the current directory
while (false !== ($entry = readdir($h))) {
    if($entry != '.' && $entry != '..') { //Skips over . and ..
        echo $entry; //Do whatever you need to do with the file
        break; //Exit the loop so no more files are read
    }
}
?>

readdir

Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem.

关于PHP:如何在不扫描整个目录的情况下从目录中获取单个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10642777/

相关文章:

php - 通过 HTTP 复制 MySQL 数据库

c++ - 我的代码停止读取我的 .txt 文件

codeigniter - 使用codeigniter删除目录

python - URL 文件夹系统 Django Python

php - 使用 min() 获取包含最小值的数组

php - mysql - woocommerce 如何获取类别中的产品

c++ - 讨厌的 unicode 和 C++ : Easy way to read ASCII/UTF-8/UTF-16 BE/LE text file

PHP 系统 ("cd\");不工作

php - 使用已弃用的 homebrew/php 在 OSX 上安装 PHP 5.6 的 PHP 扩展

c# - 从文件中提取字符串并使用 c# 保存在另一个文件中