Created
November 6, 2015 07:14
-
-
Save matt-allan/6549fc55d01ac65a2f63 to your computer and use it in GitHub Desktop.
gettings started with reading streams in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Open the stream | |
$numbers = fopen(__DIR__ . '/numbers.txt', 'r'); | |
// Wrap our parser in an infinite loop, so it won't stop until we say so | |
while (true) { | |
// Read a line | |
$buffer = fgets($numbers); | |
// The docs say it will return false when there arent any bytes left, | |
// so we break out of our loop and let the sript die when that happens. | |
if (!$buffer) { | |
break; | |
} | |
// Here you would upload it to the 'system', whatever that is. | |
// Lets just echo it to make sure our script works. | |
echo $buffer; | |
} | |
// Close the stream. PHP will do this anyway when the script ends, | |
// but it's good practice to always do it. | |
fclose($numbers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment