PHP如何接受数据流保存为图片数据并且还带有多个其他参数?

已邀请:

zkbhj - 凯冰科技站长

赞同来自:

上传文件:
 <?php
//@file phpinput_post.php
$data=file_get_contents('btn.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 = '/image.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;
}
?>
接收文件:
<?php
/**
*Recieve image data
**/
error_reporting(E_ALL);

function get_contents() {
$xmlstr= file_get_contents("php://input");
$filename=time().'.png';
if(file_put_contents($filename,$xmlstr)){
echo 'success';
}else{
echo 'failed';
}
}
get_contents();
?>

 1.APP发1.jpg,而且带有两个参数一个是假设是X和另外一个假设是Y
2.PHP负责接受X,Y和1.jpg,并且还要保存1.jpg到服务器

步骤:
1.PHP页面代码
$data = file_get_contents(‘php://input’);
//这样可以获取到未经处理的原数据(保持发送的图片流不被破坏),在APP上使用<strong>X#Y#</strong>图片流使用http发送到PHP页面
//然后PHP页面进行数据处理和分割

2.数据处理
先分割数据流
$vars = explode(“#”,$data,3);//这样防止对图片流造成破坏只分割成三份即可
/*省去若干代码*/
$img = $vars[2];
$path = ‘/var/www/uploads/’;
$newfilename = time().”.jpg”;
$file = $path.$newfilename;
$handle = fopen($file, “w”);

if ($handle) {fwrite($handle,$img);
fclose($handle);

要回复问题请先登录注册