Php 如何使用fopen同时打开两个文件?

Php 如何使用fopen同时打开两个文件?,php,foreach,fopen,Php,Foreach,Fopen,我编写了一个php脚本,它使用fopen读取日志文件并提取所有必要的信息 问题是我需要从两个文件中同时提取信息作为数组 我如何用fopen做到这一点 只需呼叫fopen和fread两次: $filename1 = "file1.log"; $handle1 = fopen($filename1); $content1 = fread($handle1, filesize($filename1)); fclose($handle1); $filename2 = "file2.log"; $han

我编写了一个php脚本,它使用fopen读取日志文件并提取所有必要的信息

问题是我需要从两个文件中同时提取信息作为数组


我如何用fopen做到这一点

只需呼叫
fopen
fread
两次:

$filename1 = "file1.log";
$handle1 = fopen($filename1);
$content1 = fread($handle1, filesize($filename1));
fclose($handle1);

$filename2 = "file2.log";
$handle2 = fopen($filename2);
$content2 = fread($handle2, filesize($filename2));
fclose($handle2);

您可以拥有所需的任意多个文件指针。你甚至可以像这样在循环中调用fopen

<?php
$files = array("file1","file2","file3");
$fps = array();
foreach($files as $fls)
$fps[] = fopen($fls);

///rest of the code.
?>


fopen返回指向文件的指针。

调用fopen两次?这意味着要加倍读取部分,这是我想要避免的。因为脚本将优先考虑数组中的第二次读取