I’d like to pass a filename through a GET request to a php script. I would have thought the following code:
<?php
$myFile = $_GET['filename'];
$file = fopen($myFile, "r");
while (!feof($myFile))
{
$currentLine = fgets($myFile);
print $currentLine;
}
?>
would work, but it is not. I’m getting the following message repeated an infinite amount of times:
Warning: feof() expects parameter 1 to be resource, string given in /Library/WebServer/Documents/maps/getFile.php on line 7
Warning: fgets() expects parameter 1 to be resource, string given in /Library/WebServer/Documents/maps/getFile.php on line 9
I know I should be wrapping my open and while loop around an IF statement to prevent the possible opening of files that do not exist, but the file I am sending EXISTS. It seems like I cannot do this with a GET request?
You need to use
$fileas the parameter forfeof()andfgets().$filerepresents your file handle resource.$myFileis just a string with your file name.