如何获取我的file_get_contents()函数来从文件中检索信息


How to get my file_get_contents() function to retrieve the information from a file?

在使file_get_contents()函数实际工作时遇到了一些困难。我使用Coda 2来练习PhP,每次我试图检索要显示的文件的内容时,都会显示如下警告:

Warning: file_get_contents(template.txt):
failed to open stream: No such file or directory in - on line 9

在第九行,我有以下内容:

echo file_get_contents("template.txt");

我验证了文件的位置,并确保它与这个PHP文件在同一个文件夹中。也许这里遗漏了一个小细节,但这应该是有效的。

感谢您提前提供的帮助。

注意:我还尝试在带有MAMP的浏览器上使用此功能,但没有成功。我目前在mac电脑上工作,如果这对我有任何帮助的话。

如果文件在同一目录中,请使用:

echo file_get_contents(__DIR__."/template.txt");

__DIR__魔术常量始终包含引用__DIR__的文件的目录路径。然而,应用程序的当前工作目录可能完全不同。

否则,如前所述,请使用文件的绝对路径。