- Example reading an entire file using
file_get_contents
// Read entire file into $file
$file = file_get_contents("myfile.txt");
- Same function can make an HTTP request and retrieve the entire response
// Read response into $homepage
$homepage = file_get_contents("http://cs.harding.edu/");
- Example reading a single line from a text file
$myfile = fopen("myfile.txt", "r") or die("Unable to open file!");
$line = fgets($myfile);
fclose($myfile);
- Reading until EOF is reached
$myfile = fopen("myfile.txt", "r") or die("Unable to open file!");
// Output one line at a time until EOF
while(!feof($myfile)) {
echo fgets($myfile) . "
\n";
}
fclose($myfile);