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.
http://www.bkjia.com/PHPjc/956983.htmlwww.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...