This article gives an example of how PHP sends and receives streaming files. Share to everyone for your reference. Specifically as follows:
sendstreamfile.php send the file as a stream
Receivestreamfile.php receive stream files and save to local
sendstreamfile.php file:
Copy Code code as follows:
<?php
/** PHP Send streaming files
* @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:
Copy Code code as follows:
<?php
/** PHP Receive stream files
* @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 (' Success ' => (BOOL) $ret));
?>
I hope this article will help you with your PHP program design.