1.用戶端模拟圖檔上傳程式(test.php):
<?php
$data=file_get_contents('1.png');
$http_entity_body = $data;
$http_entity_type = 'application/x-www-form-urlencoded';
$http_entity_length = strlen($http_entity_body);
$host = '127.0.0.1';
$port = 80;
$path = '/upload.php';
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp) {
fputs($fp, "POST {$path} HTTP/1.1\r\n");
fputs($fp, "Host: {$host}\r\n");
fputs($fp, "Content-Type: {$http_entity_type}\r\n");
fputs($fp, "Content-Length: {$http_entity_length}\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $http_entity_body . "\r\n\r\n");
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}
?>
2.服務端接收程式(upload.php):
<?php
error_reporting(E_ALL);
function get_contents() {
$xmlstr = file_get_contents('php://input')?file_get_contents('php://input') : gzuncompress($GLOBALS['HTTP_RAW_POST_DATA']);//得到post過來的二進制原始資料
$filename=time().'.png';
if(file_put_contents($filename,$xmlstr)){
echo 'success';
}else{
echo 'failed';
}
}
get_contents();
?>
執行test.php,看看你的根目錄是不是有新的圖檔了!
衆所周知,通過二進制方式的不能夠通過get,post方式拿到參數,那怎麼進行參數的傳遞呢?
答案就是讓用戶端把二進制進行一個分割組拼即可,服務端隻需要把拿到的二進制字元串進行分隔就可以得到了。
3戶端模拟圖檔上傳程式(test2php):
<?php
$data=file_get_contents('1.png');
$data = 'www.4jcms.com[x]四季企業網站系統[]'.$data;//假設服務端需要額外的兩個參數,URL以及站名,我們用“[x]”進行組合,服務端也用這個進行拆分
$http_entity_body = $data;
$http_entity_type = 'application/x-www-form-urlencoded';
$http_entity_length = strlen($http_entity_body);
$host = '127.0.0.1';
$port = 80;
$path = '/upload2php';
$fp = fsockopen($host, $port, $error_no, $error_desc, 30);
if ($fp) {
fputs($fp, "POST {$path} HTTP/1.1\r\n");
fputs($fp, "Host: {$host}\r\n");
fputs($fp, "Content-Type: {$http_entity_type}\r\n");
fputs($fp, "Content-Length: {$http_entity_length}\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $http_entity_body . "\r\n\r\n");
while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}
?>
4服務端接收程式(upload2php):
<?php
error_reporting(E_ALL);
function get_contents() {
$xmlstr = file_get_contents('php://input')?file_get_contents('php://input') : gzuncompress($GLOBALS['HTTP_RAW_POST_DATA']);//得到post過來的二進制原始資料
$arr = explode("[x]",$data,3);
$url = $arr[0]; //網址參數
$sitename = $arr[1]; //站名參數
$data = $arr[2]; //圖檔二進制字元串
$filename=time().'.png';
if(file_put_contents($filename,$data)){
echo 'success';
}else{
echo 'failed';
}
}
get_contents();
?>
運作test2.php看看,是不是就得到你想要的結果了呢。
分享到: