PHP methods for sending and receiving stream files _php Tutorial

Source: Internet
Author: User

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 ...

  • 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.