Php method for sending and receiving stream files, _ PHP Tutorial

Source: Internet
Author: User
Php sends and receives stream files ,. Php sends and receives stream files. This article describes how php sends and receives stream files. Share it with you for your reference. The details are as follows: sendStreamFile. php: how to send and receive stream files in a stream of php,

This article describes how php sends and receives stream files. 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.

Example: This article describes how php sends and receives stream files. Share it with you for your reference. The details are as follows: sendStreamFile. php streams the file...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.