Home > Backend Development > PHP Tutorial > How to send and receive stream files in php_PHP tutorial

How to send and receive stream files in php_PHP tutorial

WBOY
Release: 2016-07-13 10:07:12
Original
1161 people have browsed it

How to send and receive stream files in php

This article mainly introduces the method of sending and receiving stream files in php. It analyzes common operating techniques of php for stream files with examples. Friends in need can refer to it

The example in this article describes the method of sending and receiving stream files in PHP. Share it with everyone for your reference. The details are as follows:

sendStreamFile.php sends the file as a stream
receiveStreamFile.php receives stream files and saves them locally

sendStreamFile.php file:

The code is as follows:

/**php send stream file
* @param String $url received path
* @param String $file The file to send
* @return boolean
*/
function sendStreamFile($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 = sendStreamFile('http://localhost/receiveStreamFile.php','send.txt');
var_dump($ret);
?>

receiveStreamFile.php file:

The code is as follows:

/**php receives stream file
* @param String $file The file name saved after receiving
* @return boolean
*/
function receiveStreamFile($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));
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/956983.htmlTechArticleHow to send and receive stream files in php. This article mainly introduces the method of sending and receiving stream files in php, with examples. Analyzed the common operating techniques of PHP for streaming files. Friends in need can refer to...
source:php.cn
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