readfile returns 附加数字

readfile returns additional digit

我是 PHP 初学者。我正在尝试读取保存在本地计算机上的文件。我有连接,我可以读取文件,但 PHP 继续提供额外的数字。

这是我的代码

<html>
    <body>
        <h1>My first PHP page</h1>
        <?php
            echo readfile("/home/pi/test/hx711py/export.txt", "r");
        ?>
    </body>
</html>

另一方面,文件非常简单,只包含数字“10”。 所以输出应该只是数字 10,但我一直得到 10 3.

这是网络界面上的输出:

这是我正在尝试读取的文件:

http://php.net/manual/en/function.readfile.php

readfile() 自动回显文件内容,因此您不需要使用 echo

readfile() 也是 returns 一个整数,这就是您看到的 3。

将您的代码更改为此。

<html>
<body>
<h1>My first PHP page</h1>
<?php
readfile("/home/pi/test/hx711py/export.txt", "r");
?>
</body>
</html>

来自 PHP.net 的 "readfile" 函数的文档说:

读取文件
Returns 从文件中读取的字节数。如果发生错误,则返回 FALSE,除非该函数被调用为 @readfile(),否则将打印一条错误消息。

来源:http://php.net/manual/en/function.readfile.php


如果您想读取文件的内容并将结果作为字符串使用 file_get_contents 函数。

"file_get_contents" 函数的文档可在此处获取: http://php.net/manual/en/function.file-get-contents.php