要使用PHP读取文件内容,您可以使用以下方法之一:
方法1:使用file()函数
<?php$filename = "example.txt";$content = file($filename);if ($content !== false) { echo "<pre>"; print_r($content); echo "</pre>";} else { echo "无法读取文件内容";}?>方法2:使用fopen()和fgets()函数
<?php$filename = "example.txt";if (fopen($filename, "r")) { $content = ""; while (($line = fgets($handle)) !== false) { $content .= $line; } fclose($handle); echo "<pre>"; print_r($content); echo "</pre>";} else { echo "无法读取文件内容";}?>方法3:使用file_get_contents()函数
<?php$filename = "example.txt";if (file_get_contents($filename)) { echo "<pre>"; print_r($content); echo "</pre>";} else { echo "无法读取文件内容";}?>请注意,这些方法可能会受到文件大小和权限的限制。如果您需要处理大文件,建议使用readfile()或fread()函数。


