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>
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');