Home > Backend Development > PHP Tutorial > php sends and receives stream files

php sends and receives stream files

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-08 09:22:30
Original
878 people have browsed it

php Send and receive stream files

sendStreamFile.php Send files as streams

receiveStreamFile.php Receive stream files and save them locally

sendStreamFile.php

<?php
/** php 发送流文件
* @param  String  $url  接收的路径
* @param  String  $file 要发送的文件
* @return boolean
*/
function sendStreamFile($url, $file){

    if(file_exists($file)){

        $opts = array(
            &#39;http&#39; => 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 = sendStreamFile('http://localhost/fdipzone/receiveStreamFile.php', 'send.txt');
var_dump($ret);
?>
Copy after login

receiveStreamFile.php

<?php
/** php 接收流文件
* @param  String  $file 接收后保存的文件名
* @return boolean
*/
function receiveStreamFile($receiveFile){

    $streamData = isset($GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;])? $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;] : &#39;&#39;;

    if(empty($streamData)){
        $streamData = file_get_contents(&#39;php://input&#39;);
    }

    if($streamData!=&#39;&#39;){
        $ret = file_put_contents($receiveFile, $streamData, true);
    }else{
        $ret = false;
    }
<span style="color:#ff0000;">
</span>    return $ret;

}

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

The above introduces how to send and receive stream files in PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template