PHP:如何从目录中获取单个文件而不扫描整个目录? [英] PHP: How can I grab a single file from a directory without scanning entire directory?

查看:118
本文介绍了PHP:如何从目录中获取单个文件而不扫描整个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个130万个文件的目录,我需要移动到一个数据库。我只需要从目录中获取单个文件名,而不扫描整个目录。我抓住哪个文件无关紧要,当我完成它后,我将删除它,然后继续下一步。这可能吗?我可以找到的所有示例似乎将整个目录列表扫描到一个数组中。我只需要一次抓住一个来处理...每次不是130万。

解决方案

这应该做到这一点:

 <?php 
$ h = opendir('./'); //打开当前目录
while(false!==($ entry = readdir($ h))){
if($ entry!='。&& $ entry!=' ..'){//跳过。
echo $ entry; //做任何你需要做的文件
break; //退出循环,所以没有更多的文件被读取
}
}
?>

readdir


返回目录中下一个条目的名称。条目按文件系统存储的顺序返回。



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. It does not matter which file I grab as I will delete it when I am done with it and then move on to the next. Is this possible? All the examples I can find seem to scan the whole directory listing into an array. I only need to grab one at a time for processing... not 1.3 Million every time.

解决方案

This should do it:

<?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:如何从目录中获取单个文件而不扫描整个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆