PHP 8.5.0 Alpha 1 available for testing

stream_filter_append

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

stream_filter_appendAñade un filtro a un flujo al final de la lista

Descripción

stream_filter_append(
    resource $stream,
    string $filtername,
    int $read_write = ?,
    mixed $params = ?
): resource

stream_filter_append() añade el filtro filtername a la lista de filtros adjuntos al flujo stream.

Parámetros

stream

El flujo de destino.

filtername

El nombre del filtro.

read_write

Por omisión, stream_filter_append() añadirá el filtro a la lista de filtros de lectura si el fichero se abrió en modo lectura (r y/o +). El filtro también se adjuntará a la lista de filtros de escritura si el fichero se abrió en modo escritura (w, a y/o +). STREAM_FILTER_READ, STREAM_FILTER_WRITE, y/o STREAM_FILTER_ALL pueden también ser utilizadas en el parámetro read_write para controlar este comportamiento.

params

Este filtro se añadirá con los parámetros params al final de la lista de filtros, y será llamado al final de las operaciones de filtros. Para añadir un filtro al principio de la lista, utilice la función stream_filter_prepend().

Valores devueltos

Devuelve un recurso en caso de éxito, o false si ocurre un error. El recurso puede ser utilizado para referirse a la instancia de este filtro durante una llamada a la función stream_filter_remove().

false es devuelto si stream no es un recurso o si filtername no puede ser alcanzado.

Ejemplos

Ejemplo #1 Controlar la aplicación de los filtros

<?php
// Apertura de un fichero de prueba en modo lectura/escritura
$fp = fopen('test.txt', 'w+');

/* Se aplica el filtro ROT13 al flujo de escritura, pero no al
* de lectura */
stream_filter_append($fp, "string.rot13", STREAM_FILTER_WRITE);

/* Se añade una simple cadena al fichero, será
* transformada por ROT13 al escribir */
fwrite($fp, "Ceci est un test\n");

/* Se vuelve al principio del fichero */
rewind($fp);

/* Se lee el contenido del fichero.
* Si se aplicara el filtro ROT13 tendríamos la
* cadena en su estado original */
fpassthru($fp);

fclose($fp);

/* Resultado esperado
----------------

Guvf vf n grfg

*/
?>

Notas

Nota: Cuando se utilizan filtros personalizados
stream_register_filter() debe ser llamada antes de stream_filter_append() para registrar el filtro bajo el nombre de filtername.

Nota: Los datos del flujo (locales y remotos) son devueltos en fragmentos, los datos no procesados se conservan en el búfer interno. Cuando un nuevo filtro es añadido al final del flujo, los datos en el búfer interno son pasados al nuevo filtro en ese momento. Esto es diferente del comportamiento de stream_filter_prepend().

Nota: Cuando un filtro es añadido para lectura y escritura, se crean dos instancias del filtro. stream_filter_prepend() debe ser llamada dos veces con STREAM_FILTER_READ y STREAM_FILTER_WRITE para obtener los recursos de los filtros.

Ver también

add a note

User Contributed Notes 2 notes

up
8
Dan J
9 years ago
Note that stream filters applied to STDOUT are not called when outputting via echo or print.

This is easily demonstrated with the standard ROT13 filter:
<?php
stream_filter_append
( STDOUT, "string.rot13" );

print
"Hello PHP\n";
// Prints "Hello PHP"

fprintf( STDOUT, "Hello PHP\n" );
// Prints "Uryyb CUC"
?>

If you want to filter STDOUT, you may have better luck with an output buffering callback added via ob_start:
http://php.net/manual/en/function.ob-start.php

At the time of this writing, there is an open PHP feature request to support echo and print for stream filters:
https://bugs.php.net/bug.php?id=30583
up
4
net_navard at yahoo dot com
19 years ago
Hello firends

The difference betweem adding a stream filter first or last in the filte list in only the order they will be applied to streams.

For example, if you're reading data from a file, and a given filter is placed in first place with stream_filter_prepend()the data will be processed by that filter first.

This example reads out file data and the filter is applied at the beginning of the reading operation:

<?php
/* Open a test file for reading */
$fp = fopen("test.txt", "r");
/* Apply the ROT13 filter to the
* read filter chain, but not the
* write filter chain */
stream_filter_prepend($fp, "string.rot13",
STREAM_FILTER_READ);
// read file data
$contents=fread($fp,1024);
// file data is first filtered and stored in $contents
echo $contents;
fclose($fp);
?>

On the other hand, if stream_filter_append() is used, then the filter will be applied at the end of the data operation. The thing about this is only the order filters are applied to streams. Back to the example, it's not the same thing removing new lines from file data and then counting the number of characters, than performing the inverse process. In this case, the order that filters are applied to stream is important.

This example writes a test string to a file. The filter is applied at the end of the writing operation:

<?php
/* Open a test file for writing */
$fp = fopen("test.txt", "w+");
/* Apply the ROT13 filter to the
* write filter chain, but not the
* read filter chain */
stream_filter_append($fp, "string.rot13",
STREAM_FILTER_WRITE);
/* Write a simple string to the file
* it will be ROT13 transformed at the end of the
stream operation
* way out */
fwrite($fp, "This is a test\n"); // string data is
first written, then ROT13 tranformed and lastly
written to file
/* Back up to the beginning of the file */
rewind($fp);
$contents=fread($fp,512);
fclose($fp);
echo
$contents;
?>

In the first case, data is transformed at the end of the writing operation, while in the second one, data is first filtered and then stored in $contents.

With Regards
Hossein
To Top