我正在尝试让我的PHP脚本打开多个文本文档并阅读它们。
我目前的脚本如下:
<?php
//$searchthis = "ignore this";
$matches = array();
$FileW = fopen('result.txt', 'w');
$handle = @fopen("textfile1.txt", "r");
ini_set('memory_limit', '-1');
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(stripos($buffer, $_POST["search"]) !== FALSE)
$matches[] = $buffer;
}
fwrite($FileW, print_r($matches, TRUE));
fclose($handle);
}
?>
我试图像一堆文件一样开启,可能就像其中的8个或更少。
我如何打开并阅读所有这些文件?
非常感谢任何帮助!
答案 0 :(得分:0)
答案 1 :(得分:0)
我不知道你为什么要打开很多文件,肯定会使用大量的内存,无论如何,你可以使用file_get_contents
函数和foreach
:
$files = array("textfile1.txt", "textfile2.txt", "textfile3.txt");
$data = "";
foreach ($files as $file) {
$data .= @file_get_contents($file);
}
echo $data;
答案 2 :(得分:0)
php中有一个名为file
的函数,它将整个文件读入数组。
<?php
// "file" function creates array with each line being 1 value to an array
$fileOne = file('fileOne.txt');
$fileTwo = file('fileTwo.txt');
// Print an array or do all array magic with $fileOne and $fileTwo
foreach($fileOne as $fo) {
echo $fo;
}
foreach($fileTwo as $ft) {
$echo $ft;
}
?>
阅读更多关于:file function ion php
的信息