PHP 8.5.0 Alpha 1 available for testing

stream_get_contents

(PHP 5, PHP 7, PHP 8)

stream_get_contentsLee todo un flujo en un string

Descripción

stream_get_contents(resource $stream, ?int $length = null, int $offset = -1): string|false

stream_get_contents() es idéntica a file_get_contents(), salvo que opera sobre un puntero de fichero ya abierto y devuelve el contenido restante, hasta length bytes, en un string y comenzando en la posición offset.

Parámetros

stream (resource)

Un recurso de flujo (por ejemplo, devuelto por la función fopen())

length (entier)

El número máximo de bytes a leer. Por omisión, null (lee todo el contenido restante del buffer).

offset (entier)

Se desplaza a la posición especificada antes de la lectura. Si el número pasado es negativo, no se realizará ningún desplazamiento y la lectura comenzará desde la posición actual.

Valores devueltos

Devuelve un chaîne de caractères o false en caso de error.

Historial de cambios

Versión Descripción
8.0.0 length ahora es nullable.

Ejemplos

Ejemplo #1 Ejemplo con stream_get_contents()

<?php

if ($stream = fopen('http://www.example.com', 'r')) {
// muestra toda la página, comenzando en la posición 10
echo stream_get_contents($stream, -1, 10);

fclose($stream);
}

if (
$stream = fopen('http://www.exemple.net', 'r')) {
// Muestra los 5 primeros bytes
echo stream_get_contents($stream, 5);

fclose($stream);
}

?>

Notas

Nota: Esta función es segura binariamente.

Nota:

Cuando se especifica un valor de length distinto de null, esta función asignará inmediatamente un buffer interno de ese tamaño, incluso si el contenido real es significativamente más corto.

Ver también

  • fgets() - Recupera la línea actual a partir de la posición del puntero de archivo
  • fread() - Lectura del archivo en modo binario
  • fpassthru() - Muestra el resto del fichero

add a note

User Contributed Notes 3 notes

up
10
clarck dot smith at gmail dot com
13 years ago
In that case when stream_get_contents/fread/fgets or other stream reading functions block indefinitely your script because they don't reached the limit of bytes to read use the socket_get_meta_data function to figure out the number of the bytes to read. It returns an array that contains a key named 'unread_bytes' and then pass that number to your favourite stream reading functions second parameter to read from the stream.

Maybe a good workaround to use the stream_select function, and set the socket to non-blocking mode with the use of stream_set_blocking($stream, 0). In this case the socket reading functions work properly.

Cheers, Ervin
up
3
m rahman
14 years ago
When omitting the parameter $maxlength, any received bytes are stacked up until the underlying stream is not readable anymore, the the function returns that stack in one piece.
up
0
jure dot sah at t-2 dot com
1 month ago
In my case this is acting a little unpredictable, which may occur in your case as well depending on how the streams are set up.

This can be resolved by using the offset 0 (rather than the default -1), to ensure your streams are always read from the start, rather than the current position.
To Top