Php发送与接收流文件

数据流

  • 数据流(data stream)最初是通信领域使用的概念,代表传输中所使用的信息的数字编码信号序列。然而,我们所提到的数据流概念与此不同。这个概念最初在1998年由Henzinger在文献87中提出,他将数据流定义为“只能以事先规定好的顺序被读取一次的数据的一个序列”。百度百科参考地址

输入流与输出流

  • 数据流分为输入流(InputStream)和输出流(OutputStream)两类。输入流只能读不能写,而输出流只能写不能读。
  • 输入流可从键盘或文件中获得数据,输出流可向显示器、打印机或文件中传输数据。

PHP Streams是内置核心操作

  • PHP Streams 用于统一文件、网络、数据压缩类文件操作方式,并未这些类文件操作提供一组通用的函数接口。

  • 一个stream就是一个具有流式行为的资源对象,每个stream对象都有一个包装类。Stream 可以通过://方式来引用。其中是包装类的名字,中的内容是由包装类的语法指定,不同的包装类的语法会有所不同。

  • 内置默认的包装类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    var_dump(stream_get_wrappers());
    /*
    array(12) {
    [0] =>
    string(5) "https"
    [1] =>
    string(4) "ftps"
    [2] =>
    string(13) "compress.zlib"
    [3] =>
    string(14) "compress.bzip2"
    [4] =>
    string(3) "php"
    [5] =>
    string(4) "file"
    [6] =>
    string(4) "glob"
    [7] =>
    string(4) "data"
    [8] =>
    string(4) "http"
    [9] =>
    string(3) "ftp"
    [10] =>
    string(4) "phar"
    [11] =>
    string(3) "zip"
    }
    */
  • 获取数据方式

    1
    2
    3
    4
    5
    6
    7
    file_get_contents ( "foo.txt" );
    file_get_contents ( "file:///foo.txt" );
    file_get_contents ( "http://www.example.com/foo.txt" );
    file_get_contents ( "ftp://user:pass@ftp.example.com/foo.txt" );
    file_get_contents ( "ftp://user:pass@ftp.example.com/foo.txt" );
    file_get_contents('php://input'); //php获取流数据
    ...

stream_context_create — 创建资源流上下文

1
resource stream_context_create ([ array $options [, array $params ]] )
  • 创建并返回一个资源流上下文,该资源流中包含了 options 中提前设定的所有参数的值。
  • 实例代码
    1
    2
    //send.txt
    发送测试内容文本
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    //send.php
    function send($url, $file){

    if(file_exists($file)){

    $opts = array(
    'http' => array(
    'method' => 'POST',
    'header' => 'content-type:application/x-www-form-urlencoded',
    'content' => file_get_contents($file)
    )
    );

    $context = stream_context_create($opts);//创建资源流
    $response = file_get_contents($url, false, $context);
    $ret = json_decode($response, true);
    return $ret['success'];

    }else{
    return false;
    }

    }

    $ret = send('http://localhost/StreamFile/receive.php', 'send.txt');
    var_dump($ret);
  • 接收流
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    //receive.php
    function receive($receiveFile){

    $streamData = isset($GLOBALS['HTTP_RAW_POST_DATA'])? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

    if(empty($streamData)){
    $streamData = file_get_contents('php://input'); //读取数据流
    }

    if($streamData!=''){
    $ret = file_put_contents('接收到的内容=> '. $receiveFile, $streamData, true); //输出写入文件
    }else{
    $ret = false;
    }

    return $ret;

    }

    $receiveFile = 'receive.txt';
    $ret = receiveStreamFile($receiveFile);
    echo json_encode(array('success'=>(bool)$ret));