如何使用 PHP 和 HTML 显示文件的内容


How to display the contents of a file using PHP and HTML

所以这就是我目前在PHP方面所拥有的

<?php
if (isset($_POST['submit']))
{
    $name = $_POST['name'];
    $file = fopen("names.txt", "a+");
    fwrite($file,$name);
    fclose($file);
    $readFile = fopen("names.txt", "r");
    fread($file);
}
?>

如何在网页上显示从文件中读取的内容?

你可以用

echo $yourfile更简单的方法!

您还应该使用 readfile():

readfile("/path/to/file");

这将读取文件并通过一个命令将其发送到浏览器。这和一样。

echo file_get_contents("/path/to/file");

使用 file_get_contents()

$file = file_get_contents('names.txt', true);
echo $file;