搜尋
首頁php教程php手册php发送与接收流文件的方法,

php发送与接收流文件的方法,

本文实例讲述了php发送与接收流文件的方法。分享给大家供大家参考。具体如下:

sendStreamFile.php 把文件以流的形式发送
receiveStreamFile.php 接收流文件并保存到本地

sendStreamFile.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 接收流文件
* @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程序设计有所帮助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具