How to send and receive streaming files in PHP _php tips

Source: Internet
Author: User

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.

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.