您好,欢迎访问一九零五行业门户网

php如何发送与接收流文件

php如何发送与接收流文件?本文主要介绍了php发送与接收流文件的方法,实例分析了php针对流文件的常见操作技巧。希望对大家有所帮助。
sendstreamfile.php 把文件以流的形式发送
receivestreamfile.php 接收流文件并保存到本地
sendstreamfile.php文件:
<?php /** php 发送流文件 * @param string $url 接收的路径 * @param string $file 要发送的文件 * @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文件:
]<?php /** php 接收流文件 * @param string $file 接收后保存的文件名 * @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)); ?>
下面是其它网友的补充
php读取流文件
$filepath = 'http://www.vip.com/down'; $fp = fopen($filepath,"r"); header("content-type: application/octet-stream"); header("accept-ranges: bytes"); header("content-disposition: attachment; filename=xxx.pdf"); $buffer = 1024; while (!feof($fp)) { $file_con = fread($fp,$buffer); echo $file_con; } fclose($fp);
相关推荐:
php 文件上传的原理简单介绍
php 文件分割与合并(断点续传)
php 文件读取系列方法详解
以上就是php如何发送与接收流文件的详细内容。
其它类似信息

推荐信息