Leer archivo de texto con PHP usando búfer
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
Hola, yo soy el contenido de un archivo de texto | |
Seguramente seré leído con 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 | |
/* | |
Leer archivo de texto con PHP | |
usando búfer | |
https://parzibyte.me/blog | |
*/ | |
$nombre_archivo = "letra.txt"; | |
$gestor = fopen($nombre_archivo, "r"); # Modo r, read | |
if (!$gestor) { | |
exit("Error abriendo archivo"); | |
} | |
$tamanio_bufer = 10; # bytes | |
while (($lectura = fgets($gestor, $tamanio_bufer)) != false) { | |
// Nota: aquí podrías concatenar en una cadena, guardarlo por ahí, etcétera | |
echo "\nLeído: " . $lectura; | |
} | |
// Si el ciclo no terminó debido a un EOF (End of file) entonces | |
// algo malo ocurrió | |
if (!feof($gestor)) { | |
exit("Error al leer"); | |
} | |
// No olvides cerrar el gestor | |
fclose($gestor); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment