【发布时间】:2014-07-09 09:46:20
【问题描述】:
我想读取 php 页面上的文本文件并以相同的形式(带有段落)打印(回显)内容。所以我尝试了两种实现方式
代码 1
$textfile = "teste.txt"; // Declares the name and location of the .txt file
$content="";
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w ');
if (is_readable($textfile)) {
$content .= fread($fh, 8192);
echo $content;
} else {
echo 'The file is not readable.';
}
fclose($fh);
?>
使用此代码,屏幕上不会出现任何内容。我还有另一个问题。如果我在 fread 上使用 filesize ($textfile) 我有这个错误 Length parameter must be greater than 0.所以我猜这是问题。但文本文件有内容
代码 2
<?php
$homepage = file_get_contents('teste.txt');
echo $homepage;
?>
此代码有效。但格式不是我想要的。 回声打印这个
25 ºC 26 ºC 27 ºC 26 ºC 26 ºC 我希望每一行都出现一个值,就像我在文本文件中一样。
我能做些什么来完成它 感谢您的帮助
【问题讨论】:
-
简单:nl2br(),浏览器需要 html
-
@Dagon,它就像一个魅力!我为此研究了很多东西,但我没有找到答案。也许我问了工作问题并使用了错误的术语。非常感谢您的帮助