PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

stream_filter_prepend> <stream_encoding
Last updated: Fri, 20 Jun 2008

view this page in

stream_filter_append

(PHP 4 >= 4.3.0, PHP 5)

stream_filter_append — Attache un filtre à un flux en fin de liste

Description

resource stream_filter_append ( resource $stream , string $filtername [, int $read_write [, mixed $params ]] )

stream_filter_append() ajoute le filtre filtername à la liste de filtres attachés au flux stream . Ce filtre sera ajouté avec les paramètres spécifiés dans params à la fin de cette liste et sera donc appelé en dernier durant les opérations de flux. Pour ajouter un filtre au début de la liste, utilisez la fonction stream_filter_prepend().

Par défaut, stream_filter_append() va ajouter le filtre à la liste de filtres de lecture si le fichier a été ouvert en mode lecture (r et/ou +). Le filtre sera aussi attaché à la liste des filtres de lecture si le fichier a été ouvert en mode lecture (w, a et/ou +). STREAM_FILTER_READ, STREAM_FILTER_WRITE, et/ou STREAM_FILTER_ALL peuvent aussi être utilisées dans le paramètre read_write pour contrôler ce comportement. Voyez la fonction stream_filter_append() pour un exemple de l'utilisation de ce paramètre.

Depuis PHP 5.0.1, cette fonction retourne une ressource qui peut être utilisée pour se référer à cette instance de filtre durant l'appel à la fonction stream_filter_remove(). Dans les versions antérieures à PHP 5.1.0, cette fonction retourne FALSE en cas de succès, FALSE sinon.

Exemple #1 Contrôler l'application des filtres

<?php
// Ouverture d'un fichier de test en lecture/écriture
$fp fopen('test.txt''w+');

/* On applique le filtre ROT13 au flux d'écriture, mais pas à
 * celui de lecture */
stream_filter_append($fp"string.rot13"STREAM_FILTER_WRITE);

/* On ajoute un simple chaîne dans le fichier, il sera
 * transformé par ROT13 à l'écriture */
fwrite($fp"Ceci est un test\n");

/* On revient au début du fichier */
rewind($fp);

/* On lit le contenu du fichier.
 * Si on appliquait le filtre ROT13 nous aurions la
 * chaîne dans son étât d'origine */
fpassthru($fp);

fclose($fp);

/* Résultat attendu
   ----------------

Guvf vf n grfg

*/
?>

Note: Quand vous utilisez des filtres personnalisés stream_register_filter() doit être appelée avant stream_filter_append() pour enregistrer le filtre sous le nom de filtername .

Note: Les données du flux (locales et distantes) sont retournées en morceaux, les données non acheminées étant conservées dans le tampon interne. Lorsqu'un nouveau filtre est ajouté à la fin du flux, les données dans le tampon interne sont passées dans le nouveau filtre à ce moment-là. Ceci est différent du comportement de stream_filter_prepend().

Voir aussi stream_filter_register(), stream_filter_prepend() et stream_get_filters().



add a note add a note User Contributed Notes
stream_filter_append
dlvoy
23-Jul-2008 12:20
While using compression filters on a large set of files during one script invocation i've got
        Fatal error: Allowed memory size of xxx bytes exhausted
even when my max memory limit settings was insane high (128MB)

Workaround is to remember to remove filter after work done with stream_filter_remove:

<?php
foreach($lot_of_files as $filename)
{
   
$fp = fopen($filename, 'rb');
   
$filter_params = array('level' => 2, 'window' => 15, $memory' => 6);
    $s_filter = stream_filter_append($fp, '
zlib.deflate', STREAM_FILTER_READ, $filter_params);
    // here stream-operating code               

    stream_filter_remove($s_filter);

    fclose($fp);
}
?>
net_navard at yahoo dot com
12-Dec-2005 10:47
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

stream_filter_prepend> <stream_encoding
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites