Current Location: Home> Latest Articles> How to use stream_get_filters to select a stream filter for large files?

How to use stream_get_filters to select a stream filter for large files?

gitbox 2025-05-20

In PHP, stream filters are powerful tools that can process data in real time when reading or writing streams, such as compression, encryption, encoding, or content replacement. When we work with large files, using the right stream filter not only reduces memory consumption, but also improves processing efficiency.

This article will explain how to use the stream_get_filters function to list the available stream filters on the system and select filters suitable for large file processing based on requirements.

1?? What is stream_get_filters

stream_get_filters() is a built-in function that gets all stream filters registered in the current PHP environment. Its call is very simple:

 $filters = stream_get_filters();
print_r($filters);

This returns an array listing of available filters, for example:

 Array
(
    [0] => zlib.inflate
    [1] => zlib.deflate
    [2] => string.rot13
    [3] => string.toupper
    [4] => string.tolower
    [5] => convert.iconv.*
)

in:

  • zlib.inflate / zlib.deflate → for compression and decompression (suitable for large file compression transfer).

  • convert.iconv.* → for character encoding conversion.

  • string.* → Simple string operations (usually not suitable for large files because they are block-by-block operations, not streaming optimization).

2?? How to choose a filter suitable for large files

For large file processing, we usually focus on two types of filters:
? Compression class (zlib) : can reduce the disk read and write volume.
? Encoding conversion class (iconv) : Streaming conversion character encoding, suitable for large text files.

Example: Compress a large file content and write a new file

 $source = 'large_input.txt';
$destination = 'large_output.gz';

// Open source file
$in = fopen($source, 'rb');
// Open the target file and attach zlib.deflate Compression filter
$out = fopen('compress.zlib://' . $destination, 'wb');

if (!$in || !$out) {
    die('Unable to open the file');
}

while (!feof($in)) {
    $buffer = fread($in, 8192);
    fwrite($out, $buffer);
}

fclose($in);
fclose($out);

echo "The file has been compressed and saved as $destination\n";

Note the compress.zlib:// protocol used here. In fact, the underlying layer uses the zlib.deflate filter.

3?? Dynamically apply filters in combination with stream_filter_append

Sometimes you don't want to use a protocol wrapper, but dynamically add filters to existing streams, you can use stream_filter_append :

 $fp = fopen('output.txt', 'wb');
$filter = stream_filter_append($fp, 'string.toupper', STREAM_FILTER_WRITE);

fwrite($fp, 'Hello gitbox.net!');
fclose($fp);

// output.txt Lieutenant General Writes HELLO GITBOX.NET!

For large files, avoid too many string series filters, as they are not necessarily optimized for performance. It is recommended to give priority to filters specially designed for streaming processing, such as zlib , bzip2 , iconv .

4?? List and detect filters

If you are not sure which filters are available, you can detect this:

 $availableFilters = stream_get_filters();
$needed = ['zlib.deflate', 'convert.iconv.utf-8/cp1251'];

foreach ($needed as $filter) {
    $found = false;
    foreach ($availableFilters as $available) {
        if (stripos($available, $filter) !== false) {
            $found = true;
            break;
        }
    }
    echo $filter . ': ' . ($found ? 'Available' : '不Available') . "\n";
}