繁体   English   中英

PHP:如何在我的页面上显示文本文件的内容?

[英]PHP: How do I display the contents of a textfile on my page?

我的Web服务器上有一个.txt文件(本地),希望通过PHP echo显示页面内的内容(存储在同一台服务器上)。

.txt文件包含一个由另一个页面上的另一个脚本更新的数字,但是对于这个页面,我只想从文件中提取数字/ txt文件内容并回显它(以保存必须进行计算的页面)再次得到这个数字)。

我怎样才能做到这一点?

这是我到目前为止所得到的:

    <?php
    $myFile = "http://renownestates.com/cache/feedSubscribers.txt";
    $fh = fopen($myFile, 'r');
    $theData = fread($fh, 1);
    fclose($fh);
    echo $theData;
    ?>     

在这里,试试这个(假设它是一个小文件!):

<?php
echo file_get_contents( "filename.php" ); // get the contents, and echo it out.
?>

文档在这里

对于只读文件并输出它,最好的是readfile

如果你不想对文件中的东西做任何事情,只需显示它,你实际上可以只include()它。 include任何文件类型的工作,但当然它运行它在里面找到的任何PHP代码。

我必须显示计算机代码的文件。 如果文件中的特殊字符小于或大于,则简单的“包含”将不会显示它们。 尝试:

$file = 'code.ino';
$orig = file_get_contents($file);
$a = htmlentities($orig);

echo '<code>';
echo '<pre>';

echo $a;

echo '</pre>';
echo '</code>';

我必须使用nl2br来正确显示回车符,它对我有用:

<?php
echo nl2br(file_get_contents( "filename.php" )); // get the contents, and echo it out.
?>
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>

试试这个在php中打开一个文件

请参阅:( http://www.w3schools.com/php/showphp.asp?filename=demo_file_fopen

如果你只是想显示文件本身:

header('Content-Type: text/plain');
header('Content-Disposition: inline; filename="filename.txt"');
readfile(path);

使用PHP的fopen

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM