Kyl*_*son 13 php directory file
我有一个包含130万个文件的目录,我需要将其移动到数据库中.我只需要从目录中获取单个文件名而不扫描整个目录.我抓住哪个文件并不重要,因为当我完成它之后我将删除它然后继续下一个.这可能吗?我能找到的所有示例似乎都将整个目录列表扫描成一个数组.我只需要一次抓一个进行处理...每次不是130万.
vim*_*ist 19
这应该这样做:
<?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
}
}
?>
Run Code Online (Sandbox Code Playgroud)
返回目录中下一个条目的名称.条目按文件系统存储的顺序返回.
只需获取目录迭代器并查找作为文件的第一个条目:
foreach(new DirectoryIterator('.') as $file)
{
if ($file->isFile()) {
echo $file, "\n";
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这还可以确保您的代码在某些其他文件系统行为上执行,而不是您期望的行为.
见DirectoryIterator和SplFileInfo.