从PHP中的文本文件中读取[重复]
问题描述:
This question already has an answer here:
I am using this code to try and read the values in a text file.
$mapFile = fopen('config/map.txt', 'r') or die ("Can't open config file");
while ($line = fread($mapfile,filesize('config/map.txt')))
{
echo $line;
}
Instead I keep getting this error.
Warning: fread() expects parameter 1 to be resource, null given
I am unsure what I am doing wrong. How can I read the text in the file properly?
</div>
此问题已经存在 这里有一个答案: p>
-
”注意:未定义的变量“,”注意:未定义的索引“和”通知:未定义的偏移“使用PHP
28 answers
span>
li>
ul>
div>
我正在使用此代码尝试读取文本文件中的值。 p>
$ mapFile = fopen('config / map.txt','r')或die (“无法打开配置文件”); while($ line = fread($ mapfile,filesize('config / map.txt'))) { echo $ line; } 代码> pre>
而是我不断收到此错误。 p>
警告:fread()期望参数1为资源,null为 代码> PRE>
我不确定我做错了什么。 如何正确阅读文件中的文字? p> div>
答
PHP variables are case sensitive. $mapFile !== $mapfile
.
$mapFile = fopen('config/map.txt', 'r'); // don't need `or die()`
while ($line = fread($mapFile, filesize('config/map.txt')))
{
echo $line;
}
答
Your variable is $mapFile
, and you're trying to fread $mapfile
. Variables names in PHP are case sensitive. Also, consider file_get_contents() instead, which will open the file, get its entire contents, and close the file, in one function call.
答
You can use the following code to read full file
$file=file('config/map.txt');
foreach($file as $line)
{
echo $line;
}
Or simply
echo file_get_contents('config/map.txt');