How to send and receive stream files in php, _PHP tutorial

WBOY
Release: 2016-07-13 10:06:49
Original
993 people have browsed it

php method of sending and receiving streaming files,

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:

Copy code The code is as follows:
/**php send stream file
* @param String $url The received path
* @param String $file The file to be sent
* @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:


Copy code 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.

http://www.bkjia.com/PHPjc/957140.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/957140.htmlTechArticleHow to send and receive stream files in php. 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 streams the file...
Related labels:
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