PHP: How can I grab a single file from a directory without scanning entire directory?

前端 未结 4 500
别跟我提以往
别跟我提以往 2020-12-29 08:59

I have a directory with 1.3 Million files that I need to move into a database. I just need to grab a single filename from the directory WITHOUT scanning the whole directory.

4条回答
  •  孤独总比滥情好
    2020-12-29 09:15

    Just obtain the directories iterator and look for the first entry that is a file:

    foreach(new DirectoryIterator('.') as $file)
    {
        if ($file->isFile()) {
            echo $file, "\n";
            break;
        }        
    }
    

    This also ensures that your code is executed on some other file-system behaviour than the one you expect.

    See DirectoryIterator and SplFileInfo.

提交回复
热议问题