This article mainly introduces php's method of sending and receiving stream files, and analyzes common php operations on stream files, for more information about how to send and receive stream files in php, see the following example. Share it with you for your reference. The details are as follows:
SendStreamFile. php sends the file as a stream
ReceiveStreamFile. php receives the stream file and saves it locally
SendStreamFile. php file:
The code is as follows:
<? Php
/** Php sends a stream file
* @ Param String $ path received by the url
* @ 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
/** Php receives 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 (array ('success '=> (bool) $ ret ));
?>
I hope this article will help you with php programming.