Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 13, 2019 01:47
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 parzibyte/76a9f58f5486fe5a7dbef0521a98fa88 to your computer and use it in GitHub Desktop.
Save parzibyte/76a9f58f5486fe5a7dbef0521a98fa88 to your computer and use it in GitHub Desktop.
Leer archivo de texto con PHP usando búfer
Hola, yo soy el contenido de un archivo de texto
Seguramente seré leído con PHP
<?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