我有一个包含 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
}
}
?>
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/