if ($file = fopen("file.txt", "r")) {
while(!feof($file)) {
$line = fgets($file);
# do same stuff with the $line
}
fclose($file);
}
11
您可以使用 fgets() 函数逐行读取文件:
$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
}
fclose($handle);
} else {
// error opening the file.
}
<?php
$file = new SplFileObject("file.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
// Echo one line from the file.
echo $file->fgets();
}
// Unset the file to call __destruct(), closing the file handle.
$file = null;
24
如果您要打开一个大文件,您可能希望使用生成器和fgets()来避免将整个文件加载到内存中:
/**
* @return Generator
*/
$fileData = function() {
$file = fopen(__DIR__ . '/file.txt', 'r');
if (!$file)
die('file does not exist or cannot be opened');
while (($line = fgets($file)) !== false) {
yield $line;
}
fclose($file);
};
像这样用它:
foreach ($fileData() as $line) {
// $line contains current line
}
这样您就可以处理foreach()中的各个文件行 .
注意:生成器需要> = PHP 5.5
4
使用缓冲技术来读取文件 .
$filename = "test.txt";
$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
$buffer = fread($source_file, 4096); // use a buffer of 4KB
$buffer = str_replace($old,$new,$buffer);
///
}
70
有一个 file() 函数返回文件中包含的行数组 .
foreach(file('myfile.txt') as $line) {
echo $line. "\n";
}
595
foreach (new SplFileObject(__FILE__) as $line) {
echo $line;
}
function parse_file($filename)
{
try {
$file = new SplFileObject($filename);
} catch (LogicException $exception) {
die('SplFileObject : '.$exception->getMessage());
}
while ($file->valid()) {
$line = $file->fgets();
//do something with $line
}
//don't forget to free the file handle.
$file = null;
}
2
函数读取数组返回
function read_file($filename = ''){
$buffer = array();
$source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
while (!feof($source_file)) {
$buffer[] = fread($source_file, 4096); // use a buffer of 4KB
}
return $buffer;
}
13 回答
您可以使用
fgets()
函数逐行读取文件:您可以为文件使用面向对象的接口类 - SplFileObject http://php.net/manual/en/splfileobject.fgets.php(PHP 5> = 5.1.0)
如果您要打开一个大文件,您可能希望使用生成器和fgets()来避免将整个文件加载到内存中:
像这样用它:
这样您就可以处理foreach()中的各个文件行 .
注意:生成器需要> = PHP 5.5
使用缓冲技术来读取文件 .
有一个
file()
函数返回文件中包含的行数组 .小心'while(!feof ... fgets()'的东西,fgets可以得到一个错误(returnfing false)并永远循环而不会到达文件的末尾.codaddict最接近正确但是当你的'while fgets'时循环结束,检查feof;如果不是true,那么你有一个错误 .
这个问题的流行解决方案之一将涉及新行字符的问题 . 使用简单的
str_replace
可以很容易地修复它 .这是我如何管理非常大的文件(测试高达100G) . 它比fgets()更快
所有答复都没有明显的答案 . PHP有一个整洁的流分隔符解析器,可用于此目的 .
在处理大型文件时,SplFileObject非常有用 .
函数读取数组返回