Streaming Access in PHP

Source: Internet
Author: User
Tags file system ini posix requires resource safe mode wrapper wrappers

Access stream

All file I/O processing in the PHP user space is handled through the PHP flow wrapper layer introduced in PHP 4.3. Internally, the extension code can optionally use Stdio or POSIX file processing and local file system or Berkeley domain sockets for communication, or it can invoke the same APIs as user space flow I/O.

Overview of Streams

Typically, a direct file descriptor consumes less CPU and memory than the calling flow wrapper layer; However, this will accumulate all the work that implements a particular protocol to you as an extension developer. By hooking up to the flow wrapper layer, your extension code can transparently use a variety of built-in flow wrappers, such as HTTP, FTP, and their corresponding SSL versions, plus gzip and bzip2 compression packs. By including specific pear or PECL modules, your code can also access other protocols, such as SSH2, WebDav, or even gopher!

This chapter describes the underlying APIs that are internally based on streaming work. Later in the 16th chapter, "Interesting streams," we will see high-level concepts such as application filters, use of contextual options and parameters.

Open stream

Although it is a unified API, it actually relies on the type of stream you want, and there are four different paths to open a stream. From a user-space perspective, these four different categories are as follows (the function list is only represented as an example, not a complete list):

<?php  
    /* fopen Packing 
     * Operation file/uri method specify remote File class resource
    /$fp = fopen ($url, $mode);  
    $data = file_get_contents ($url);  
    File_put_contents ($url, $data);  
    $lines = file ($url); 
     /* Transmission * Based on the sequential I/O socket/
    $fp = Fsockopen ($host, $port);  
    $fp = Stream_socket_client ($uri);  
    $fp = Stream_socket_server ($uri, $options);  
      
    /* Directory Flow *
    /$dir = Opendir ($url);  
    $files = Scandir ($url);  
    $obj = Dir ($url);  
      
    /* "Special" flow * * *
    $fp = tmpfile ();  
    $fp = Popen ($cmd);  
    Proc_open ($cmd, $pipes);  
>

No matter what type of flow you open, they are stored in a public structure php_stream.

fopen Packaging

We begin by implementing the fopen () function. Now that you're familiar with creating an extension skeleton, if you're not familiar with it, go back to chapter 5th "Your first extension" review, and here's the fopen () function we implemented:

Php_function (Sample5_fopen)  
{  
    php_stream *stream;  
    Char *path, *mode;  
    int Path_len, Mode_len;  
    int options = Enforce_safe_mode | report_errors;  
      
    if (Zend_parse_parameters (Zend_num_args () TSRMLS_CC, "SS",  
            &path, &path_len, &mode, &mode_len) = = Failure) {return  
        ;  
    }  
    stream = Php_stream_open_wrapper (path, mode, options, NULL);  
    if (!stream) {  
        return_false;  
    }  
    Php_stream_to_zval (stream, return_value);  

The purpose of php_stream_open_wrapper () should be to completely bypass the ground floor. path Specifies that you want to read or write a filename or URL, and the read and write behavior depends on the value of mode.

Options is a collection of tagged values for a bit field, which is set to the set of fixed values described below:

Use_path applies the include_path in the php.ini file to the relative path. The built-in function fopen () sets this option when you specify the third argument to be true.

Stream_use_url This option is set, only the remote URL will be opened. For php://, file://, zlib://, bzip2://these URL wrappers do not consider them to be remote URLs.

Enforce_safe_mode Although this constant is named, in fact Setting this option only enables a mandatory check of Safe mode (the Safe_mode directive in the php.ini file). If this option is not set, the Safe_mode check is skipped (regardless of how safe_mode is set in the INI setting)

Report_errors when an error is encountered during the specified resource opening, an error report is generated if this option is set.

Stream_must_seek for some streams, such as sockets, are not seek (random access); This type of file handle can be seek only under certain circumstances. If the invocation scope specifies this option, and the wrapper detects that it is not guaranteed to seek, it will refuse to open the stream.

Stream_will_cast if the calling scope requires the stream to be converted to a stdio or POSIX file descriptor, this option should be passed to the Open_wrapper function to ensure that the I/O operation fails before it occurs stream_only_get_ The headers identity requires only metadata to be requested from the stream. This is actually used for the HTTP wrapper, to get the http_response_headers global variable and not really crawl the remote file content.

Stream_disable_open_basedir similar to Safe_mode check, do not set this option will check the INI settings open_basedir, if you specify this option can bypass this default check

Stream_open_persistent tells the flow wrapper layer that all internally allocated space is persisted and the associated resources are registered in the persistent list.

Ignore_path if not specified, the default include path is searched. Most URL wrappers ignore this option.

When Ignore_url provides this option, the flow wrapper layer only opens local files. All Is_url wrappers will be ignored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.