Skip to content

Instantly share code, notes, and snippets.

@matt-allan
Created November 6, 2015 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-allan/6549fc55d01ac65a2f63 to your computer and use it in GitHub Desktop.
Save matt-allan/6549fc55d01ac65a2f63 to your computer and use it in GitHub Desktop.
gettings started with reading streams in PHP
<?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