How PHP sends and receives stream files
This article mainly introduces the method of PHP sending and receiving stream files, the example analyzes the common operation skills of PHP for streaming files, the need for friends can refer to the following
This article describes how PHP sends and receives stream files. Share to everyone for your reference. Specific as follows:
sendstreamfile.php sending a file in the form of a stream
receivestreamfile.php receive stream file and save to local
sendstreamfile.php file:
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:
The code is as follows:
/** PHP Receive 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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/956983.html www.bkjia.com true http://www.bkjia.com/PHPjc/956983.html techarticle how to send and receive stream files in PHP this article mainly introduces the method of PHP sending and receiving stream files, the example analyzes the common operation skills of PHP for streaming files, and needs friends to refer ...