Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php显示.txt文件_Php_Html_Css_Text - Fatal编程技术网

用php显示.txt文件

用php显示.txt文件,php,html,css,text,Php,Html,Css,Text,我有一个自动生成的.txt文件,如下所示: SamAccountName Name PasswordLastSet -------------- ---- --------------- spiderman Parker, Peter 7/7/2019 11:18:20 PM ironman Stark, Tony 1/17/2006 4:49:32 AM There are 2 users. 我现在使

我有一个自动生成的.txt文件,如下所示:

SamAccountName Name           PasswordLastSet     
-------------- ----           ---------------    
spiderman      Parker, Peter  7/7/2019 11:18:20 PM
ironman        Stark, Tony    1/17/2006 4:49:32 AM

There are 2 users.
我现在使用以下方式将其显示到我的网站中:

<?php
    $file = 'c:\display.txt';
    $f = fopen($file, "r") or exit("Unable to open file!");
    while(!feof($f))
    {
        echo fgets($f)."<br />";
    }
    fclose($f);
?>
这样,很难正确阅读表格。 为什么会这样?是什么导致HTML/PHP读取文件的方式与实际不同?

您可以添加标签


您可以获取readfile并设置正确的标题

<?php
echo "<pre>";
readfile("display.txt");
echo "</pre>";


因此,您需要像堆栈溢出一样格式化它,就在这里,在第一个问题块中。(→ 检查元素)您应该展示您尝试的内容,并解释其“不起作用”的原因。从目前的情况来看,根据网站指南,这是“太宽泛了”(如果需要查看,请参阅)。您的第一个代码似乎可以工作,但整个网站的格式都很简单。如何仅使用我的文本进行此操作?一个网站的内容类型只能设置一次。如果网站需要内容类型text/html,则必须使用变量。
<?php
$file = 'c:\display.txt';
$f = fopen($file, "r") or exit("Unable to open file!");
echo "<pre>";
while(!feof($f))
{
    echo fgets($f);
}
echo "</pre>";
fclose($f);
<?php
header('Content-Type:text/plain');
readfile("display.txt");
<?php
echo "<pre>";
readfile("display.txt");
echo "</pre>";

<?php

header('Content-Type:text/plain');


$file = new SplFileObject("display.txt");
while (!$file->eof()) {
    echo $file->fgets().PHP_EOL;
}