PHP: How do I display the contents of a textfile o

2019-01-25 02:33发布

I have a .txt file on my web server (locally) and wish to display the contents within a page (stored on the same server) via PHP echo.

The .txt file contains a number that is updated by another script on another page, but for this page I just want to pull the number/txt file contents from the file and echo it (to save the page having to do the calculation involved in getting the number again).

How can I do this?

Here's what I've got so far:

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

标签: php echo
8条回答
Melony?
2楼-- · 2019-01-25 02:54

If you aren't looking to do anything to the stuff in the file, just display it, you can actually just include() it. include works for any file type, but of course it runs any php code it finds inside.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-25 03:00

For just reading file and outputting it the best one would be readfile.

查看更多
狗以群分
4楼-- · 2019-01-25 03:06

Here, try this (assuming it's a small file!):

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

Documentation is here.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-25 03:07

Use PHP's fopen

查看更多
Viruses.
6楼-- · 2019-01-25 03:17

I have to display files of computer code. If special characters are inside the file like less than or greater than, a simple "include" will not display them. Try:

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

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

echo $a;

echo '</pre>';
echo '</code>';
查看更多
Luminary・发光体
7楼-- · 2019-01-25 03:19

I had to use nl2br to display the carriage returns correctly and it worked for me:

<?php
echo nl2br(file_get_contents( "filename.php" )); // get the contents, and echo it out.
?>
查看更多
登录 后发表回答