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);
}
?>
stream_filter_append
(PHP 4 >= 4.3.0, PHP 5)
stream_filter_append — Süzgeç zincirinin sonuna bir süzgeç ekler
Açıklama
$akım
, string $süzgeç
[, int $oku_yaz
[, mixed $değiştirgeler
]] )
akım akımına ekli süzgeç zincirinin sonuna
süzgeç isimli süzgeci ekler.
Değiştirgeler
-
akım -
Hedef akım.
-
süzgeç -
Süzgeç ismi.
-
read_write -
stream_filter_append() işlevi öntanımlı olarak, dosya okumak için (r veya r+ kipi) açılmışsa süzgeci okuma süzgeci zincirine ekler. Eğer dosya yazmak için (w, a veya w+, a+ kipi) açılmışsa, süzgeç yazma süzgeci zincirine de eklenir. Bu davranışı geçersiz kılmak için değiştirgeye
STREAM_FILTER_READ,STREAM_FILTER_WRITEve/veyaSTREAM_FILTER_ALLsabitleri de aktarılabilir. -
değiştirgeler -
Süzgeç, belirtilen
değiştirgelerile listenin sonuna eklenir ve akım işlemlerinde son çağrılan süzgeç olur. Süzgeci listenin başına eklemek için stream_filter_prepend() işlevini kullanın.
Dönen Değerler
stream_filter_remove() çağrısına aktarılmak üzere bir özkaynak döndürür.
Sürüm Bilgisi
| Sürüm: | Açıklama |
|---|---|
| 5.1.0 |
PHP 5.1.0 öncesinde, bu işlev başarı durumunda TRUE yoksa FALSE
döndürürdü.
|
Örnekler
Örnek 1 - Süzgeçlerin uygulanacağı yerin belirlenmesi
<?php
/* Okumak ve yazmak için bir dosya açalım */
$fp = fopen('test.txt', 'w+');
/* ROT13 süzgecini yazma zincirine ekleyelim
fakat okuma zincirine eklemeyelim */
stream_filter_append($fp, "string.rot13", STREAM_FILTER_WRITE);
/* ROT13 ile dönüştürülmek üzere dosyaya bir şeyler yazalım */
fwrite($fp, "This is a test\n");
/* Dosyanın başına dönelim */
rewind($fp);
/* Dosya içeriğini okuyalım.
* Süzgeç okuma zincirine de uygulanmış
* olsaydı metni eski durumuna dönüştürülmüş
* olarak görecektik. */
fpassthru($fp);
fclose($fp);
/* Beklenen çıktı
---------------
Guvf vf n grfg
*/
?>
Notlar
Bilginize: Özel (kullanıcı) süzgeçlerini kullanırken
İstenen kullanıcı süzgecinisüzgeçolarak tanıtmak için ilk olarak stream_filter_register() çağrılmalıdır.
Bilginize: Henüz okunmamış veri dahili tamponlarda tutulurken akım verisi özkaynaklardan (hem yerel hem de uzak) büyük kümeler halinde okunur. Süzgeç zincirinin sonuna yeni bir süzgeç eklendiğinde, dahili tamponlardaki işlenmiş veriler yeni süzgeçten de geçirilir. Bu davranış, yeni süzgeci zincirin başına ekleyen stream_filter_prepend() işlevinden bu yönüyle farklıdır.
Bilginize: Bir süzgeç hem okuma hem de yazma amacıyla eklendiğinde iki örneği oluşturulur. Her iki süzgeç özkaynağını ayırmak için stream_filter_append() işlevi
STREAM_FILTER_READveSTREAM_FILTER_WRITEile ayrı ayrı çağrılmalıdır.
Ayrıca Bakınız
- stream_filter_register() - Kullanıcı tanımlı bir akım süzgecini etkin kılar
- stream_filter_prepend() - Süzgeç zincirinin başına bir süzgeç ekler
- stream_get_filters() - Etkin süzgeçlerin listesini döndürür
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
