search
HomeBackend DevelopmentPHP TutorialPHP Master | Understanding Streams in PHP

PHP Master | Understanding Streams in PHP

Core points

  • PHP streaming is a powerful tool for generalizing file, network and data compression operations. They can be read or written linearly and can be fseek() anywhere in the stream. Each stream has a wrapper for handling specific protocols or encodings.
  • PHP provides built-in wrappers, protocols, and filters, and allows the creation and registration of custom wrappers, protocols, and filters. The default wrapper is file://, which is used every time you access the file system. Other wrappers include wrappers for HTTP, Amazon S3, MS Excel, Google Storage, Dropbox, and Twitter.
  • Stream context is a stream-specific parameter or option that can modify and enhance the behavior of the wrapper. They can be used to avoid the use of cURLs in simple network operations and can be modified or created individually for different purposes. Streams can be used to create virtual file system wrappers for PaaS providers and can be used to build custom wrappers and filters for applications that implement custom file formats and encodings.

PHP streams are resources provided by PHP and we often use them transparently, but they can also be very powerful tools. By learning how to harness their power, we can take our applications to a higher level. The PHP manual has a good description of streaming: > Streaming was introduced in PHP 4.3.0 as a way to generalize files, networks, data compression, and other operations that share a common set of functions and uses. Simply put, a stream is a resource object that exhibits streamable behavior. That is, it can be read or written linearly and may be able to fseek() anywhere in the stream.

Each stream has an implementation wrapper that contains additional code needed to handle a specific protocol or encoding. PHP provides some built-in wrappers that we can easily create and register custom wrappers. We can even use context and filters to modify or enhance the behavior of the wrapper.

Flow basics

The reference format of the

stream is <scheme>://<target></target></scheme>. <scheme></scheme> is the name of the wrapper, and <target></target> will vary according to the syntax of the wrapper. The default wrapper is file://, which means we use streams every time we access the file system. For example, we can write readfile('/path/to/somefile.txt') or readfile('file:///path/to/somefile.txt') and get the same result. If we use readfile('http://google.com/') instead, then we tell PHP to use the HTTP stream wrapper. As mentioned earlier, PHP provides some built-in wrappers, protocols, and filters. To understand what wrappers are installed on our machine, we can use:

<?php
print_r(stream_get_transports());
print_r(stream_get_wrappers());
print_r(stream_get_filters());
?>

My installation output is as follows:

<?php
print_r(stream_get_transports());
print_r(stream_get_wrappers());
print_r(stream_get_filters());
?>

A good set of tools, isn't it? Additionally, we can write or use third-party streams for Amazon S3, MS Excel, Google Storage, Dropbox and even Twitter.

php:// wrapper

PHP has its own wrapper to access the language's I/O stream. There are basic php://stdin, php://stdout and php://stderr wrappers map the default I/O resources, and we also php://input, a read-only stream with the original body of the POST request. This is very convenient when we deal with remote services that put the data payload inside the POST request body. Let's use cURL for quick test:

<code>Array
(
    [0] => tcp
    [1] => udp
    [2] => unix
    [3] => udg
    [4] => ssl
    [5] => sslv3
    [6] => sslv2
    [7] => tls
)
Array
(
    [0] => https
    [1] => ftps
    [2] => compress.zlib
    [3] => compress.bzip2
    [4] => php
    [5] => file
    [6] => glob
    [7] => data
    [8] => http
    [9] => ftp
    [10] => zip
    [11] => phar
)
Array
(
    [0] => zlib.*
    [1] => bzip2.*
    [2] => convert.iconv.*
    [3] => string.rot13
    [4] => string.toupper
    [5] => string.tolower
    [6] => string.strip_tags
    [7] => convert.*
    [8] => consumed
    [9] => dechunk
    [10] => mcrypt.*
    [11] => mdecrypt.*
)</code>

The result of print_r($_POST) in response to the PHP script will be:

curl -d "Hello World" -d "foo=bar&name=John" http://localhost/dev/streams/php_input.php

Please note that the first packet cannot be accessed from the $_POST array. However, if we use readfile('php://input') instead, we will get:

Array
(
    [foo] => bar
    [name] => John
)

PHP 5.1 introduces php://memory and php://temp stream wrappers for reading and writing temporary data. As the name implies, data is stored in memory or in temporary files managed by the underlying system, respectively. There is also php://filter, a meta wrapper designed to apply filters when opening streams using functions such as readfile() or file_get_contents()/stream_get_contents().

<code>Hello World&foo=bar&name=John</code>

The first example uses a filter to encode the data written to the disk, while the second example applies two cascading filters to read data from a remote URL. The result can range from very basic to very powerful in our application.

Flow context

Context is a set of stream-specific parameters or options that can modify and enhance the behavior of our wrappers. A common use context is to modify HTTP wrappers. This allows us to avoid using cURL in simple network operations.

<?php
// 写入编码数据
file_put_contents("php://filter/write=string.rot13/resource=file:///path/to/somefile.txt","Hello World");

// 读取数据并进行编码/解码
readfile("php://filter/read=string.toupper|string.rot13/resource=http://www.google.com");
?>

First, we define an array of options, which is an array of arrays in the format $array['wrapper']['option_name'] (the available context options vary depending on the specific wrapper). Then we call stream_context_get_default(), which returns the default context and accepts an optional array of options to apply. The readfile() statement uses these settings to get content. In this example, the content is sent in the body of the request, so the remote script will use php://input to read it. We can access the header using apache_request_headers() and get:

<?php
$opts = array(
    'http' => array(
        'method' => "POST",
        'header' => "Auth: SecretAuthToken\r\n" .
                    "Content-type: application/x-www-form-urlencoded\r\n" .
                    "Content-length: " . strlen("Hello World"),
        'content' => 'Hello World'
    )
);
$default = stream_context_get_default($opts);
readfile('http://localhost/dev/streams/php_input.php');
?>

We modified the default context option, but we can also create alternative contexts for use alone.

Array
(
    [Host] => localhost
    [Auth] => SecretAuthToken
    [Content-type] => application/x-www-form-urlencoded
    [Content-length] => 11
)

Conclusion

How do we use the power of flow in the real world? Where else can we go? As we can see, streams share some or all the file system-related functions, so the first use that comes to my mind is a series of virtual file system wrappers for PaaS provisioning with Heroku or AppFog, such as Heroku or AppFog. Use together. With little effort, we can port our applications from standard hosting services to these cloud services and enjoy the benefits. Additionally – I will show in a subsequent post – we can build custom wrappers and filters for our applications that implement custom file formats and encodings.

(The FAQs part is omitted here due to space limitations.)

The above is the detailed content of PHP Master | Understanding Streams in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor