php-streams扩展学习

  一. streams是干嘛的:
       用于统一文件、网络、数据压缩等类文件操作方式,并为这些类文件操作提供一组通用的函数接口。
 
  二. stream是具有流式行为的资源对象,这个对象有一个包装类 例如:
     print_r(stream_get_wrappers());//返回所有可用流包装器的名称
    /*
        Array
        (
          [0] => php   /** 它是PHP用来处理IO流的包装类,如php://stdin php://stdout **/
          [1] => file
          [2] => glob
          [3] => data
          [4] => http
          [5] => ftp
          [6] => zip
          [7] => compress.zlib
          [8] => https
          [9] => ftps
          [10] => phar
        )
    */
  
  三. PHP的默认包装类就是file:// ;; 所以readfile('/path/to/somefile.txt')或者readfile('file:///path/to/somefile.txt'),这两种方式是等效的
        
  四. 注册自己的包装器(自定义 协议处理器和流),用于所有其它的文件系统函数中(例如 fopen() , fread() 等)
    
  五. PHP还可以通过context和filter对包装类进行修饰和增强。
      1. 关于context,如PHP通过stream_context_create()来设置获取文件超时时间
        $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60) );
        $context = stream_context_create($opts);
        $html =file_get_contents('http://www.jb51.net', false, $context);
        
       2. 关于filter过滤器,首先来看看PHP有哪些内置的过滤器:
           print_r(stream_get_filters());
            /*
            Array
            (
              [0] => convert.iconv.*
              [1] => mcrypt.*
              [2] => mdecrypt.*
              [3] => string.rot13
              [4] => string.toupper
              [5] => string.tolower
              [6] => string.strip_tags
              [7] => convert.*
              [8] => consumed
              [9] => dechunk
              [10] => zlib.*
            )
            */
       3. 自定义的过滤器   通过stream_filter_register()和内置的php_user_filter可创建自定义的过滤器
原文地址:https://www.cnblogs.com/sixiong/p/5882670.html