php發(fā)送與接收流文件的方法
本文實(shí)例講述了php發(fā)送與接收流文件的方法。分享給大家供大家參考。具體如下:
sendStreamFile.php 把文件以流的形式發(fā)送
receiveStreamFile.php 接收流文件并保存到本地
sendStreamFile.php文件:
<?php
/** php 發(fā)送流文件
* @param String $url 接收的路徑
* @param String $file 要發(fā)送的文件
* @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));
?>
下面是其它網(wǎng)友的補(bǔ)充
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);
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- 前端實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳(前端文件提交+后端PHP文件接收)
- 前端js實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳 后端PHP文件接收
- PHP接收App端發(fā)送文件流的方法
- PHP使用curl模擬post上傳及接收文件的方法
- ThinkPHP中url隱藏入口文件后接收alipay傳值的方法
- android文件上傳示例分享(android圖片上傳)
- Android中發(fā)送Http請(qǐng)求(包括文件上傳、servlet接收)的實(shí)例代碼
- Android實(shí)現(xiàn)上傳文件功能的方法
- android 上傳文件到服務(wù)器代碼實(shí)例
- Android上傳文件到Web服務(wù)器 PHP接收文件
相關(guān)文章
php下判斷數(shù)組中是否存在相同的值array_unique
今天在改一個(gè)N久以前寫的程序 突然碰到一個(gè)問題 假設(shè)有一個(gè)數(shù)組$a中存在幾個(gè)value 我如何判斷這些value當(dāng)中是否存在相同的值呢? 翻了好多資料,也問了兵哥哥,給我一些思路,想自己寫來著~~~ 還是不肯放棄百度,最后搞了一次,居然找到這么一個(gè)函數(shù) array_unique爽大了。2008-03-03
linux環(huán)境apache多端口配置虛擬主機(jī)的方法深入介紹
本篇文章是對(duì)linux環(huán)境apache多端口配置虛擬主機(jī)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php安全之直接用$獲取值而不$_GET 字符轉(zhuǎn)義
php安全之直接用$獲取值而不$_GET 字符轉(zhuǎn)義,需要的朋友可以參考下2012-06-06
PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能,涉及php針對(duì)mysql常見的連接、數(shù)據(jù)表查詢、遍歷、table表格構(gòu)成等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
php獲取當(dāng)前月與上個(gè)月月初及月末時(shí)間戳的方法
這篇文章主要介紹了php獲取當(dāng)前月與上個(gè)月月初及月末時(shí)間戳的方法,涉及php針對(duì)日期與時(shí)間相關(guān)判斷與操作技巧,需要的朋友可以參考下2016-12-12

