php 用php读取纯文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4103287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read a plain text file with php
提问by DomingoSL
I have a text file with this information in my server:
我的服务器中有一个包含此信息的文本文件:
Data1
Data2
Data3
.
.
.
DataN
How do I read all the information from the text file (line by line) using PHP?
如何使用 PHP 从文本文件(逐行)中读取所有信息?
回答by Payload
<?php
$fh = fopen('filename.txt','r');
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
// echo($line);
}
fclose($fh);
?>
This will give you a line by line read.. read the notes at php.net/fgetsregarding the end of line issues with Macs.
这会给你一行一行的阅读……阅读php.net/fgets 上关于 Mac 的行尾问题的注释。
回答by zzzzBov
http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.explode.php
$array = explode("\n", file_get_contents($filename));
$array = explode("\n", file_get_contents($filename));
This wont actually read it line by line, but it will get you an array which can be used line by line. There are a number of alternatives.
这实际上不会逐行读取它,但它会为您提供一个可以逐行使用的数组。有多种选择。
回答by subosito
You can also produce array by using file:
您还可以使用文件生成数组:
$array = file('/path/to/text.txt');
回答by Ullas Prabhakar
$filename = "fille.txt";
$fp = fopen($filename, "r");
$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
print_r($lines);
In this code full content of the file is copied to the variable $content
and then split it into an array with each newline character in the file.
在此代码中,文件的完整内容被复制到变量中$content
,然后将其拆分为一个数组,其中包含文件中的每个换行符。
回答by DaiYoukai
W3Schools is your friend: http://www.w3schools.com/php/func_filesystem_fgets.asp
W3Schools 是您的朋友:http: //www.w3schools.com/php/func_filesystem_fgets.asp
And here: http://php.net/manual/en/function.fopen.phphas more info on fopen including what the modes are.
在这里:http: //php.net/manual/en/function.fopen.php有更多关于 fopen 的信息,包括模式是什么。
What W3Schools says:
W3Schools 说:
<?php
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
fopen opens the file (in this case test.txt with mode 'r' which means read-only and places the pointer at the beginning of the file)
fopen 打开文件(在这种情况下 test.txt 使用模式 'r' 这意味着只读并将指针放在文件的开头)
The while loop tests to check if it's at the end of file (feof) and while it isn't it calls fgets which gets the current line where the pointer is.
while 循环测试以检查它是否在文件末尾(feof),如果不是,则调用 fgets 获取指针所在的当前行。
Continues doing this until it is the end of file, and then closes the file.
继续这样做直到它是文件的结尾,然后关闭文件。
回答by Svinjica
Try something like this:
尝试这样的事情:
$filename = 'file.txt';
$data = file($filename);
foreach ($data as $line_num=>$line)
{
echo 'Line # <b>'.$line_num.'</b>:'.$line.'<br/>';
}
回答by Sani Kamal
$file="./doc.txt";
$doc=file_get_contents($file);
$line=explode("\n",$doc);
foreach($line as $newline){
echo '<h3 style="color:#453288">'.$newline.'</h3><br>';
}
回答by MPG
You can read a group of txt files in a folder and echo the contents like this.
您可以读取文件夹中的一组txt文件,并像这样回显内容。
<?php
$directory = "folder/";
$dir = opendir($directory);
$filenames = [];
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if($type !== 'file') continue;
$filenames[] = $filename;
}
closedir($dir);
?>
回答by Dimuthu Lakshan
$aa = fopen('a.txt','r');
echo fread($aa,filesize('a.txt'));
$a = fopen('a.txt','r');
while(!feof($a)){echo fgets($a)."<br>";}
fclose($a);