Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中动态搜索文件_Php - Fatal编程技术网

在php中动态搜索文件

在php中动态搜索文件,php,Php,我基本上想在我的电脑上搜索一个文件。我知道如何搜索,但是有没有一种快速搜索名称和扩展名的方法,而不必在我的脚本中填充超过2个for循环?名称(如文件、病毒、节点、杂项)和扩展名将动态添加,而不是通过源代码添加。。这些名字将有数千个,所以我不想一个接一个地添加它们,所以我会动态地添加它们 我在想: foreach($names as $i){ if (file_exists($i.".exe" | $i.".py" | $i.".js" | $i.".html" | )){ //ech

我基本上想在我的电脑上搜索一个文件。我知道如何搜索,但是有没有一种快速搜索名称和扩展名的方法,而不必在我的脚本中填充超过2个for循环?名称(如文件、病毒、节点、杂项)和扩展名将动态添加,而不是通过源代码添加。。这些名字将有数千个,所以我不想一个接一个地添加它们,所以我会动态地添加它们

我在想:

foreach($names as $i){
  if (file_exists($i.".exe" | $i.".py" | $i.".js" | $i.".html" | )){
    //echo true or false}}
就像我说的。我还将动态添加扩展。是否值得让
文件的代码很长(2万个扩展名)
或者只是用表单添加它们


它是另一个for循环中的for循环吗?

有一种使用Spl迭代器的方法

以下示例摘自的文档。它搜索名为“Yourfile.py”的文件:

$filename = 'Yourfile.py';

$directory = new RecursiveDirectoryIterator('path/to/search_root/');
$iterator = new RecursiveIteratorIterator($directory);
$regexiterator = new RegexIterator(
    $iterator,
    '/^' . preg_quote($filename) . '$/',
    RecursiveRegexIterator::GET_MATCH
);

foreach($regexiterator as $fileinfo) {
    var_dump($fileinfo);
}

<>如果你的代码在Linux服务器上,而程序“定位”,我会考虑使用它。它将为您的服务器节省大量负载,并将更快地返回结果。

您想搜索整个文件系统还是只搜索某个目录/子目录?您多久进行一次此搜索?文件多久会更改一次?特定目录,我正在寻找概念证明。您是否想要一个只针对特定操作系统的解决方案?如何搜索.py文件?我太笨了,无法理解正则表达式目前是如何工作的。我添加了
$filename
变量。现在应该更容易使用了。即使不知道正则表达式